Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Script debounce does not work properly and instakills the player [?]

Asked by 1 year ago

Hey scripters! My script that decreases player's health on touch of an npc doesn't work properly - it insta kills player with no debounce (i scripted it) Heres the script:

local RemoteEvent = game.ReplicatedStorage.RemoteEvents.ActivateJumpscareEvent
local debounce = false
script.Parent.Humanoid.Touched:Connect(function(hit)
    if not debounce then
        debounce = true
        local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
        RemoteEvent:FireClient(Player)
        Player.Character:WaitForChild("Humanoid").Health = Player.Character:WaitForChild("Humanoid").Health - 25
        wait(1)
        debounce = false
    end
end)

The script is server script. Thanks!

3 answers

Log in to vote
0
Answered by 1 year ago

I'm pretty sure it's not working because you have it as local debounce instead of globally. Local variables I think go back to their default values when the script is run again. So that's probably your issue here. Try setting the local debounce = false to debounce = false

Hope that answers your question.

Ad
Log in to vote
2
Answered by
k3du53 162
1 year ago

Do you see any errors in the output related to 'attempt to index nil with Character'?

I believe that the NPC is constantly registering .Touched events with other BaseParts around them, which is causing the debounce to reset to false as soon as it enables.

Try checking if the hit.Parent is a player before you enable the debounce.

Something like:

local RemoteEvent = game.ReplicatedStorage.RemoteEvents.ActivateJumpscareEvent
local debounce = false

script.Parent.Humanoid.Touched:Connect(function(hit)
    local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if Player and not debounce then
        debounce = true
        RemoteEvent:FireClient(Player)
        Player.Character:WaitForChild("Humanoid").Health = Player.Character:WaitForChild("Humanoid").Health - 25
        wait(1)
        debounce = false
    end
end)
Log in to vote
0
Answered by 1 year ago

Lol I am sorry i just noticed I had two scripts in one humanoid which kills the player (minus 100 HP) and which damages him (-25 every 3 sec) Also I had an error that broke the script with these events. So in conclusion I can say that whoever you are always use output window and solve errors and dont let the scripts conflict!

Answer this question