My script only works one time

I was making one boss experience ( game ), I made this script, and turns out at the first time, it works, at the second time, it doesn’t. I tried a debounce, but it didn’t fix the problem. Any help?

Script:

if debounce == false then
humanoid.Died:Connect(function()
if humanoid.Health == 0 then
debounce = true
for i, players in pairs(game.Players:GetPlayers()) do
players.leaderstats.Points.Value += cash
wait(2)
sound.Playing = true
wait(3)
sound.Playing = false
debounce = false
end
end
end)
end

2 Likes

I do not believe the full code is shown here, but my guess would be that when the character respawns, you do not hook your function to the new humanoid.

When a character dies in ROBLOX, the old character is destroyed, and a new character takes it’s place. So for your code you would need to listen to when the character is added, and then find the Humanoid in the new character and hook your Died function to it.

1 Like

That means I have to put CharacterAdded on it, right?

FullScript:

local humanoid = game.Workspace.Boss:FindFirstChild(“Humanoid”)
local cash = 100
local leaderstats = game.ServerScriptService.leaderstats
local sound = game.Workspace.cashregistersound
local debounce = false
local boss = game.Workspace.Boss
if debounce == false then
humanoid.Died:Connect(function()
if humanoid.Health == 0 then
debounce = true
for i, players in pairs(game.Players:GetPlayers()) do
players.leaderstats.Points.Value += cash
wait(2)
sound.Playing = true
wait(3)
sound.Playing = false
debounce = false
end
end
end)
end

1 Like

How is your Boss respawning? You’ll have to hook up this connection to each new Boss as they are respawned.

It has a respawn script inside the boss model. For example, If I move the boss to the red brick, then the respawn point will be moved to the red brick. ( Red brick is an example )

I would disable respawning until the full script is gone all the way through, then having them spawn after the whole script is finished

Well your example script is only connected to the first one it finds.

Also a question, what is sound? is it a dying sound?

because with the way you have it, it will play for everyone in the server, if you dont want it like that i can help you with it

If the boss will be killed, then everyone would get 100 points. Plus, the money sound effect will be played after that.

So I have a better way I would script it, here is how I would do it:

local humanoid = game.Workspace.Boss:FindFirstChild(“Humanoid”)
local cash = 100
local leaderstats = game.ServerScriptService.leaderstats
local sound = game.Workspace.cashregistersound
local debounce = false
local boss = game.Workspace.Boss
if debounce == false then
humanoid.Died:Connect(function()
if humanoid.Health == 0 then
debounce = true
for i, players in pairs(game.Players:GetPlayers()) do
players.leaderstats.Points.Value += cash
end
wait(2) -- I would recommend deleting this but you dont have to
sound.Playing = true
wait(sound.length)
sound.Playing = false
debounce = false
end
end)
end

Edit: Basically the script it is first rewarding everyone, then it plays the sound

1 Like

If you need me to explain more in depth how this would work differently let me know.

You aren’t fixing the right problem here, his Boss dies and then the script is over. It will never loop or connect to the next Boss Humanoid.

With this it first rewards all the players instantly, there isnt any wait anymore in the rewarding area.

oh never mind I see what they did.

"length is not a valid member of Sound “Workspace.cashregistersound” Is length do anything?

update to this, I would use a while wait() function to keep it in a loop.

Whoops my bad, I was thinking of animations. I dont think sound uses length, so I would keep it as the original wait time

also a question, where is this script?