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

Why are my variables turning nil?

Alright, so long story short I have a game in which I want players to not be able to jump, normally. Now, I've ran into the situation in which I would like to add benches/seats in which the players can well, sit... The catch is that if players do sit, and jumping is disabled, they'd have to reset in order to be able to move again.

Now obviously, I do not want this, instead what I tried to do was make a script that detects when the player sits, which then allows the player to jump and after they do disables it again. I've finished 2/3's of that goal, I've made my bench and inside it is a script that detects when a player sits on it, I've also been able to re-enable jumping once the script detects that a player is sitting, however the part in which I'm having issues with is re-disabling the jump after the player jumps/stands up again, and currently the output is blaming my variables for being nil... Here, take a look at the script:

local seat = script.Parent
local character = nil
local player = nil

print("Debugging- ESU Script initialized.")

seat.Changed:connect(function(property)
if property ~= "Occupant" then return end

local occupant = seat.Occupant
if occupant then
print("Debugging- 'if occupant' check went thru.")

	local character = occupant.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
		
	if player then
		player.PlayerScripts.DisableJumping.Disabled = true
		character.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
		print("Debugging- 'If player' check went thru.")
	end

else		
	
	-- Must fix this part V
	player.PlayerScripts.DisableJumping.Disabled = false
	character.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)

end	
	
end)

So, everything works up until "else", the output tells me that the "player" variable is nil, but the thing I don't understand is why since I set its value prior to that. If anybody knows what is the mistake I'm making I'll appreciate some input, and if there's a fix/way to do what I'm aiming for please let me know, any help is appreciated, thanks in advance!

2 answers

✓ Accepted answer

read here: http://wiki.roblox.com/index.php?title=Scope&redirect=no

local seat = script.Parent
local character = nil
local player = nil

print("Debugging- ESU Script initialized.")

seat.Changed:connect(function(property)
	if property ~= "Occupant" then return end
	local occupant = seat.Occupant
	if occupant then
		print("Debugging- 'if occupant' check went thru.")
	    character = occupant.Parent
    	player = game.Players:GetPlayerFromCharacter(character)
	    if player then
			player.PlayerScripts.DisableJumping.Disabled = true
        	character.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
        	print("Debugging- 'If player' check went thru.")
    	end
	else        
	    player.PlayerScripts.DisableJumping.Disabled = false
	    character.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
	end 
end)

"for loops, while loops, repeat until loops, functions, if then, and do end chunks all create a new variable scope." This means that if you set a local variable inside any of the above, that the rest of the script cannot read it. Creating the variable 'local character = ocupant.Parent' in the 'if occupant then' argument doesn't allow for the rest of the script to see that variable, you must refer to character as 'character = occupant.Parent', not 'local character = occupant.Parent' if you want it to change the character variable specified at the start of the script. Hope this helps

Excuse my gigantic stupidity, all I had to do was make the local variable a default variable, so instead of having local player = ... All I had to was type in player = ... Welp.

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