After resetting, script runs multiple times

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)

And here is the Output:


 17:27:28.466  0  -  Client - DJump:26
  17:27:29.000  Landed!  -  Client - DJump:17
  17:27:29.934  Died  -  Client - DJump:35
  17:27:37.250   ▶ 0 (x2)  -  Client - DJump:26
  17:27:37.783  Landed!  -  Client - DJump:17
  17:27:38.635  Died  -  Client - DJump:35
  17:27:43.748   ▶ 0 (x3)  -  Client - DJump:26
  17:27:44.283  Landed!  -  Client - DJump:17
  17:27:45.219  Died  -  Client - DJump:35
  17:27:49.683   ▶ 0 (x4)  -  Client - DJump:26
  17:27:50.216  Landed!  -  Client - DJump:17
2 Likes

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)

1 Like

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.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.