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

Script doesn't work in-game, faulty variable?

So, I have a game in-which you cannot jump, and I also have a bench in which you need to jump to "stand up". As you may have noticed, if you're sitting down in a chair and jumping is disabled you'll need to reset in order to leave, but I don't want this... Instead, I've made a script that detects when a player sits down, who the player is, allows them to jump and then detects when the player leaves to re-disable their jumping abilities.

I've completed that task, or at least it seem'd. The script works in Studio perfectly, players cannot normally jump, once they sit they can jump, once they jump and get out of the chair they cannot jump again, but the catch is that it won't work in-game. Take a look at the script so you can get a more clear understanding of the situation.

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

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

	local occupant = seat.Occupant
	if occupant then

		character = occupant.Parent
		player = game.Players:GetPlayerFromCharacter(character)
		playerS = player:WaitForChild("PlayerScripts")	
		
		print("Debugging: Variables have been created.")	
		
		if playerS then
			playerS.DisableJumping.Disabled = true
			character.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
			print("Debugging: Player should be able to jump now (Bench benchmark).")
		end

	else		
	
		playerS.DisableJumping.Disabled = false
		character.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)

	end	
	
end)

Now, again, this script works in studio and prints out all of the "debugs", but when I play the actual game on the platform and I try to sit down, jumping is not re-enabled. I looked at the developer console and there were no errors, however there was a warning of the possibility of infinite yield relating to this line:

playerS = player:WaitForChild("PlayerScripts")

That's currently the only thing the developer console is giving normally. Now, when I reset while sitting the developer console errors and says that there was an attempt to index an upvalue called playerS (A nil value), but currently I'm not worried about that instead I'm wondering why the script won't move on from line 14 even though the "WaitForChild()" should be completed judging from the fact PlayerScripts should've loaded a long time ago, I feel as if I'm missing something here. If anybody knows or has any idea what's going on here please feel free to let me know, any help is appreciated!

Side note: If I attempt to reference PlayerScripts directly (playerS = player.PlayerScripts) instead of using a "WaitForChild()" the developer console errors and says that PlayerScripts is not a valid member of Player.

1 comment

0

Maybe, you either have the script in the wrong service, or you have the wrong type of script. I've found that the studio seems to be more lax with that than in the game.

1 answer

✓ Accepted answer

Reading the top of this page http://wiki.roblox.com/index.php?title=API:Class/PlayerScripts it states that PlayerScripts are not accessible to the server. If you need a bool on each player that you set via your script, you can just add a BoolValue straight to the player object through code, rather than into PlayerScripts.

For example, somewhere else in your code in a server script, once you have a reference to player:

local disableJumping = Instance.new("BoolValue")
disableJumping.Name = "DisableJumping"
disableJumping.Value = true
disableJumping.Parent = player

Then in your script above:

player.DisableJumping.Value = false

1 comment

0

Not the most clear answer, but it does its job. This general knowledge will be great to have, thank you for your input, working on a fix right away!

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