Value inconsistency

Hello devforum,
Today’s post is a speed run, my variable (which is being changed inside of a function) isn’t being recognized outside of the function, instead being recognized as the value i defined when I started the script:

THE CODE:
reloading = 0 – what the value is defaulting to, declared script-start

mouse.TargetFilter = workspace

local function activated()
	if reloading == true
	then
		print('sneed')
	else
		print(reloading) -- prints 0
		shootevent:FireServer(mouse.hit.p)
		wait(.09)
		Text1.Text = workspace:FindFirstChild(tostring(player))['AK-47'].Ammo.Value
	end
end

while mousedown == true
do
	print("E")
end

local function equipped()
	local function reloading(input)
		if input.KeyCode == Enum.KeyCode.R
		then
			reloading = true --what the value SHOULD be
			updateAmmo:FireServer()
			wait(.09)
			Text1.Text = workspace:FindFirstChild(tostring(player))['AK-47'].Ammo.Value
			Text2.Text = workspace:FindFirstChild(tostring(player))['AK-47'].AmmoReservoir.Value
			print(reloading)
			wait(5)
			reloading = false
			print(reloading) -- the prints inside of this function give out the correct values
		end
	end

You’re giving the function the same name as the variable. When you localize the function to the scope (**local** function), that makes reloading restricted to the current scope too; it’s just like any other variable.

Also, on an unrelated note:

Clocking in at 0 hours, 2 minutes, and 37.41 seconds. New WR. VOD will be available soon. :wink:

3 Likes

Thanks, worked like a charm :+1:, i’ll add for the record that all I had to do was change the function name to reload

1 Like