I am making a double jump script, but Whenever I reset, My script runs multiple time after. I have tried using Debounce, but the script would break not allowing the script to function as it does without debounce. This is all done from a Local script.
Here is the Local Script:
local uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local script = script
local jump = false
local djump = false
local function charadded(character)
local humanoid = character:WaitForChild("Humanoid")
local function statechange(old, new)
if new == Enum.HumanoidStateType.Landed then
print("Landed!")
humanoid.JumpHeight = 7.2
curjump = 0
end
end
local function onjevent(input, gamep)
if not gamep then
if input.KeyCode == Enum.KeyCode.Space then
print(curjump)
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
statechange()
return
end
end
end
local function ondied()
print("Died")
end
humanoid.Died:Connect(ondied)
uis.InputBegan:Connect(onjevent)
humanoid.StateChanged:Connect(statechange)
end
player.CharacterAdded:Connect(charadded)
I assume this is in either StarterPlayerScripts, or StarterGUI, The reason its doing this is because you arent disconnecting the CharacterAdded event.
For a double jump script, I’d say place it into StarterCharacterScripts, If it cant be there, then do
local uis = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local script = script
local jump = false
local djump = false
local function charadded(character)
local humanoid = character:WaitForChild("Humanoid")
local function statechange(old, new)
if new == Enum.HumanoidStateType.Landed then
print("Landed!")
humanoid.JumpHeight = 7.2
curjump = 0
end
end
local function onjevent(input, gamep)
if not gamep then
if input.KeyCode == Enum.KeyCode.Space then
print(curjump)
humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
statechange()
return
end
end
end
uis.InputBegan:Connect(onjevent)
humanoid.StateChanged:Connect(statechange)
end
local connection = player.CharacterAdded:Connect(charadded)
local function ondied()
print("Died")
if connection ~= nil then
connection:Disconnect()
end
end
humanoid.Died:Connect(ondied)
Note: this is untested, you’ll probably have to modify it. (also, only use it if you dont wanna place your current script into StarterCharacterScripts)
Unfortunately, because of the way he has anonymous functions created inside charadded(), specifically this:
it’s not going to be enough to just disconnect charadded on death. The code will still rack up UserInputService bindings that probably start throwing errors when the previous characters Humanoid gets garbage collected (since there’s a humanoid upvalue in onjevent).
What I recommend to new developers is to always use the return value from Connect(), like you’ve shown for CharacterAdded. This forces you to think about the lifetime of the RBXScriptConnection. If your event comes from a persistent instance (e.g. LocalPlayer or UserInputService) rather than one that gets garbage collected (e.g. the Humanoid instance in each spawned character on death) it will remain connected until you disconnect it.
The persistence of the same LocalPlayer instance for the whole session is why you have this problem. Connecting to CharacterAdded doesn’t replace an existing connection if there is one, it binds a new copy of the charadded function each time you respawn, along with all the connections that function creates.