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

Is there a way for no jumping unless in a chair?

I want players to not be able to jump but still be able to get out of chairs so here is the code I tried to but together but I'm not good at coding so I need help

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:connect(function(character)
	
while true do
			wait(1)
if game.Players.LocalPlayer.Character.Humanoid.Sit == true then
	character:WaitForChild("Humanoid").JumpPower = 1
	character.Humanoid.Jump = true
	
	print("sitting")
else
	print("standing")
	character:WaitForChild("Humanoid").JumpPower = 0
	end
	end
	end)
end)

can anyone tell me what's wrong with this?

1 comment

0

Can you please tell me if this is a LocalScript or a ServerScript?

1 answer

✓ Accepted answer

Improvements

Use GetService for the Players Service

Instead of the while loop, you can use the StateChanged event of Humanoid to check for a player being seated or when they stop being seated

The default JumpPower is 50, you may not be seeing visible changes since your value is so low

Rather than forcing them to jump, just switch their JumpPower so they can choose when to, after which you can set the JumpPower back to 0 as they are no longer seated

Use local variables and be consistent in order to improve readability and debugging

Issues

LocalPlayer cannot be used on the Server

Revised Server Script

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local human = character:WaitForChild("Humanoid")
		human.JumpPower = 0
		
		human.StateChanged:Connect(function(old, new)
			if new == Enum.HumanoidStateType.Seated then
				human.JumpPower = 50
			elseif old == Enum.HumanoidStateType.Seated then
				human.JumpPower = 0
			end
		end)
	end)
end)

1 comment

0

thank you so much thats all I needed

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