Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why those variables won't update?

Asked by 10 years ago

My "Player" has some BoolValues stored inside. There is a LocalScript called "keyboardTrack" that constantly keeps changing their values every time the player press or unpress "w", "a", "s", "d,", "space", and "LShift". There it is:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local holdingW = player.keyboard.holdingW.Value
local holdingA = player.keyboard.holdingA.Value
local holdingS = player.keyboard.holdingS.Value
local holdingD = player.keyboard.holdingD.Value
local holdingShift = player.keyboard.holdingShift.Value
local holdingSpace = player.keyboard.holdingSpace.Value

function onKeyDown(key)

    if key == "w" then holdingW = true end
    if key == "a" then holdingA = true end
    if key == "s" then holdingS = true end
    if key == "d" then holdingD = true end
    if key == " " then holdingSpace = true end
    if (key:byte() == 48) then holdingShift = true end

end

function onKeyUp(key)

    if key == "w" then holdingW = false end
    if key == "a" then holdingA = false end
    if key == "s" then holdingS = false end
    if key == "d" then holdingD = false end
    if key == " " then holdingSpace = false end
    if (key:byte() == 48) then holdingShift = false end

end

mouse.KeyDown:connect(onKeyDown)
mouse.KeyUp:connect(onKeyUp)

The thing is that there is another LocalScript stored in the Player's "Character" that creates local variables pointing to the same BoolValues, but they won't update as the "keyboardTrack" change them! Why?

1 answer

Log in to vote
0
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
10 years ago

The variables do update. The variable is set to the value of the boolvalue, but not to the boolvalue itself. If the boolvalue's value is true, the variable will be set to true, not to the boolvalue. They aren't connected.

EDIT:

So instead of writing

x=BoolValue.Value
x=true

expecting BoolValue.Value to switch from false to true, write this:

x=BoolValue
x.Value=true

OR, you could get crazy with function environments and create a pseudo-pointer that returns BoolValue.Value when you try to read it and returns BoolValue when you try to set its Value:

do
    local a={}
    setfenv(1,setmetatable({},{
        __index=function(t,k)
            local r=a[k]or loadstring('return '..k)()
            pcall(function()
                if r.ClassName=="BoolValue"then
                    return r.Value
                end
            end)
            return r
        end,s
        __newindex=function(t,k,v)
            local stop=false
            pcall(function()
                if a[k].ClassName=="BoolValue"then
                    a[k].Value=v
                    stop=true
                end
            end)
            if stop then return end
            a[k]=v
        end
    }))
end
local x=BoolValue
x=false
print(BoolValue.Value)
x=true
print(BoolValue.Value)

prints false and then true.

The downside to this is that all you can do is set BoolValue.Value and read it without saying .Value, you can't touch the BoolValue in any other way like setting its parent, it won't understand what you're trying to do. Most likely, though, you aren't going to go to this extent to accomplish what you're actually trying to do like I just have.

0
so, instead of using local x = "BoolValue Path", I would use only the path? Isn't there a way to make pointers or something? Mattews123 20 — 10y
0
Thanks for that answer, it really helped me out ;) Mattews123 20 — 10y
Ad

Answer this question