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
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.
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
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 )
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