Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why those variables won't update?

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

✓ Accepted answer

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.

2 comments

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?

0

Thanks for that answer, it really helped me out ;)

This discussion is preserved as it was. New answers, comments, and votes are closed.