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

Why can the player jump over and over?

I'm trying to nerf jumping so you can only jump every two seconds. But you can repeatedly jump over and over and I can't figure out why.

plr = game.Players.LocalPlayer
chr = plr.Character or plr.CharacterAdded:wait()
humanoid = chr.Humanoid
debounce = false

humanoid.Changed:connect(function(j)
	 if j == 'Jump' then
		if j == true and debounce == false then
			debounce = true
			wait(2)
			debounce = false
		elseif j == true and debounce == true then
			j = false
		end
	end
end)

It's a localscript

2 answers

Well, you sorta have the right idea. You're using your debounce method correctly, but just not actually setting the humanoids jump property. You also don't need the "j == true" or "j == false", since we've already established that jump == "Jump".

Other than that, nothing else seems to be wrong here. Try practicing with this:

local Player = game:GetService'Players'.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Humanoid = Character.Humanoid
local Enabled = true

Humanoid.Changed:connect(function(Jump)
	if Jump == 'Jump' then
		if Enabled == true then -- We don't need to check "Jump" anymore.
			Enabled = false -- Disabled, which brings us to our elseif statement.
			wait(2)
			Enabled = true -- Enable again.
		elseif Enabled == false then
			Humanoid[Jump] = false -- Changes the property of the humanoid that was changed
		end
	end
end)

In all honesty, I'm not sure why your script doesn't work. However, I developed my own solution to help fix your problem.

What I'm doing is using the 'Jumping' event of humanoid in order to do the detection on whether or not the character should be allowed to jump, using very similar logic to what you used. Then I use the 'Changed' event and if the property that changed was jump and the debounce is true then it sets the jump to false.

Why this works and yours does not I do not know, but none the less it accomplishes the task.

local plr = game.Players.LocalPlayer
local chr = plr.Character or plr.CharacterAdded:wait()
local humanoid = chr.Humanoid
local debounce = false

humanoid.Changed:connect(function(property)
	if property == "Jump" and debounce then
		humanoid.Jump = false
	end
end)

humanoid.Jumping:connect(function(boolean)
	if boolean and not debounce then
		debounce = true
		wait(2)
		debounce = false
	end
end)

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