Help with double jump script

Hello, I made a double jump system looking at youtube tutarials.

The problem is that I want to make it possible so only I can double jump and I am not excatly sure how it is done or where to put it. I know it has if and something but idk anything else.

Here is the code

local localPlayer = game.Players.LocalPlayer
local character
local humanoid
 
local canDoubleJump = false
local hasDoubleJumped = false
local oldPower
local TIME_BETWEEN_JUMPS = 0.2
local DOUBLE_JUMP_POWER_MULTIPLIER = 2
 
function onJumpRequest()
	if not character or not humanoid or not character:IsDescendantOf(workspace) or
	 humanoid:GetState() == Enum.HumanoidStateType.Dead then
		return
	end
	
	if canDoubleJump and not hasDoubleJumped then
		hasDoubleJumped = true
		humanoid.JumpPower = oldPower * DOUBLE_JUMP_POWER_MULTIPLIER
		humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
	end
end
 
local function characterAdded(newCharacter)
	character = newCharacter
	humanoid = newCharacter:WaitForChild("Humanoid")
	hasDoubleJumped = false
	canDoubleJump = false
	oldPower = humanoid.JumpPower
	
	humanoid.StateChanged:connect(function(old, new)
		if new == Enum.HumanoidStateType.Landed then
			canDoubleJump = false
			hasDoubleJumped = false
			humanoid.JumpPower = oldPower
		elseif new == Enum.HumanoidStateType.Freefall then
			wait(TIME_BETWEEN_JUMPS)
			canDoubleJump = true
		end
	end)
end
 
if localPlayer.Character then
	characterAdded(localPlayer.Character)
end
 
localPlayer.CharacterAdded:connect(characterAdded)
UserInputService.JumpRequest:connect(onJumpRequest)```
3 Likes

At the top of your script you could have a simple condition, like so:

if localPlayer.UserId ~= your_user_id then script:Destroy() end

That should work. :slight_smile:

That didn’t work tough the output said this

Players.KundumcuFase.PlayerScripts.DoubleJump:1: attempt to index global ‘localPlayer’ (a nil value)

This is because you need to define localPlayer which is your player, you can do this by adding another line of code above the previous mentioned code.

local localPlayer = game:GetService("Players").LocalPlayer

What this does is creates a new variable, access the ‘Players’ service and get the player you are playing as.

2 Likes

omg Thx a lot you explained it really clearly and I totally understood it thx. The script works now btw.

1 Like

Can I ask you one more thing pls? How can I make it so there is a command and then it enables it.
For examle I write ;djump in chat and then I can double jump and then if I write it again I can’t double jump?

1 Like

You’d need a moderate understanding of string manipulation and Lua (the programming language Roblox uses)

Typically, you can’t expect people on here to write you up entire scripts or modified scripts, but I’m not saying it doesn’t happen.

I’d recommend digging into a tutorial series if you’re interested in developing on Roblox, it’s also a kickstart for if you desire to dig into more complex engines like Unity or maybe Unreal, so I’d say it’s definitely worth it. (I’ve only really fully learned Lua and it’s given me great insight on programming all together, and it all makes much more sense to me now than when I first started.)

Hope this helps! :slight_smile:

3 Likes