NumberValue is not changing in Roblox Workspace

醉酒当歌 提交于 2021-01-29 19:01:27

问题


I made this script for a bomb that explodes if a value isn't ten, but the value does not change in workspace and even if I change it manually to 10 the bomb still explodes anyways.

The bomb has a button to add one to the value and a button to enter, which is supposed to cause it to either explode or not explode but it always explodes.

this is the script for the enter button:

local Beans = workspace.BigBoomRoom.bomb.button3.Beans.Value

function AwMan()

end

function Creeper()
local explosion = Instance.new("Explosion")
 explosion.Parent = game.Workspace
 explosion.Position = script.Parent.Parent.Position
 explosion.BlastRadius = 25
 explosion.BlastPressure = 9999999999999

end


function onClick(click) 
    if Beans == 10
    then
        do AwMan()
            end

    else
        do Creeper()
        end
    end
    end

script.Parent.MouseClick:connect(onClick)

this is the separate script for the +1 button:

local Bean = workspace.BigBoomRoom.bomb.button3.Beans.Value
function onClick(click) 
     Bean = Bean + 1
end

script.Parent.MouseClick:connect(onClick)

The value never changes in workspace and the bomb always explodes, so both scripts must somehow be wrong.


回答1:


When you save Beans.Value into a local variable, you are not keeping a reference to the original NumberValue object, you are copying its value into the local variable. In both of these scripts, you are accessing and modifying a variable and not the original object, so changes are never saved back to the object in the Workspace.

In your +1 script, you are updating the value of Bean, but you are never saving that back to Beans NumberValue in the Workspace. It's better to instead keep a reference to the NumberValue like this.

-- hold onto the NumberValue object instead of its value
local Beans = workspace.BigBoomRoom.bomb.button3.Beans

local function onClick(playerWhoClicked)
    Beans.Value = Beans.Value + 1
end

script.Parent.MouseClick:Connect(onClick)

The same trick will fix up your bomb script :

local Beans = workspace.BigBoomRoom.bomb.button3.Beans

local function AwMan()
    print("Awwww maaaaan")
end

local function Creeper()
    print("hissss")
    local explosion = Instance.new("Explosion")
    explosion.Parent = game.Workspace
    explosion.Position = script.Parent.Parent.Position
    explosion.BlastRadius = 25
    explosion.BlastPressure = 9999999999999
end


local function onClick(playerWhoClicked)
    print("Clicking on the bomb button. Beans = ", Beans.Value)
    if Beans.Value == 10 then
       AwMan()
    else
        Creeper()
    end
end

script.Parent.MouseClick:Connect(onClick)


来源:https://stackoverflow.com/questions/57845478/numbervalue-is-not-changing-in-roblox-workspace

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!