variables - Roblox Lua : I can't do arithmetic in the middle of print()

标签 variables math lua roblox

我试图将 Rich 作为变量放在 print("You are"+ print(Rich + 1) + "Rich") 的中间。它在一个函数中,所以每当玩家做出一个部分时,在这种情况下这将是金钱,他们的财富就会增加一个:

    function createPart()
        local Rich = 0 
        local myPart = Instance.new("Part")
        print("You are" + print(Rich + 1) + "Rich")
        myPart.Transparency = 0.5
        myPart. Anchored = true
        myPart.Position = Vector3.new(0,0,0)
        myPart.CanCollide = true
        myPart.Name = "lol" -- how to name a part after instance.new
        myPart.Parent = game.Workspace 
    end
    createPart()

我坚持这一点。我能得到它如何运作的解释吗?

最佳答案

如果你想增加变量,你必须将它存储在更高的范围内,否则它会在函数调用结束时被垃圾回收。

local Rich = 0
local function createPart()
    Rich = Rich + 1
    -- print out the message
    -- create the part
end

至于打印出来,您可以通过三种不同的方式完成:

1) 逗号分隔。

print() 函数接受任意数量的参数,并将它们的值放在一起。

print("You are", Rich,"Rich")

2) 字符串连接

在lua中,..操作符用于字符串连接,而不是+操作符。

print("You are" .. tostring(Rich) .. "Rich")

3) 字符串格式化

lua 字符串库允许您通过指定变量在字符串中的位置来格式化字符串。 %d 可以用来表示一个整数。

print(string.format("You are %d Rich", Rich))

关于variables - Roblox Lua : I can't do arithmetic in the middle of print(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61699849/

相关文章:

javascript - 是否可以让 jquery 选择器接受变量?

c++ - 极端大/小数编程

lua - 设置动态模式匹配器的方法

function - 查找表中元素的每个组合(Lua/伪代码)

c++ - Lua C++ 类绑定(bind)垃圾回收关闭

javascript - 执行javascript函数,变量中存储哪个名称?

JavaScript,找不到变量

JavaScript 常量

algorithm - 查找字符串的子字符串

java - 绘画用的 Canvas 是圆形; Canvas 内的随机点