My script only works one time

You will need to know how your respawn script works, does it recreate the entire Boss model? How is the entire Boss model destroyed? Or do they build up. I’d check in the respawn script and also look in the workspace as you’re playing to see what is getting cloned. Also, your script you moved in there will probably need a WaitForChild in case some items aren’t created yet when the script runs.

1 Like

seems like you guys know what your talking about so im just going to go back to developing

1 Like

I have no clue I’ve just like started developing again this month this is my first time on here in ages :rofl:

1 Like

I think you just got programmer rank lol ( off topic )

1 Like

I’ve had it ages just this flair thingy was not a thing when I joined

1 Like

I’m more of a web developer now so I’ll keep this one :rofl:

1 Like

did they ever get a solution? Because ive been gone for over a hour lol

Found a few issues:

  1. Humanoid.Died is unreliable. Per API reference, works locally only (not a server side NPC)
  2. If the humanoid has died, is there any point in checking health?
  3. Should check for health “<=” to 0
  4. As already mentioned, nothing trying to identify the respawn boss.

Here’s how I’d do it. This script would go in ServerScriptService. Not tested, as I don’t have a boss.:

local RunService = game:GetService("RunService")
local cash = 100
local leaderstats = game.ServerScriptService.leaderstats
local sound = game.Workspace.cashregistersound
local boss = nil

local function bossDied ()
	for i, players in pairs(game.Players:GetPlayers()) do
		players.leaderstats.Points.Value += cash
		wait(2)
		sound.Playing = true
		wait(3)
		sound.Playing = false
	end
end

RunService.Heartbeat:Connect(function ()
	if game.Workspace:FindFirstChild("Boss") then
		boss = game.Workspace.Boss.Humanoid
		if boss and boss.Health <= 0 then
			coroutine.wrap(bossDied)
            boss:Destroy()
		end
	end
end)

EDIT: forgot about all the waits in the died function… have to use a coroutine I think.

Tested this, and found another error "ServerScriptService.Script:20: attempt to index nil with ‘Health’. " and the error starts to print alot after i killed the boss.

Even though if I should change to WaitForChild. It still won’t work since it gets warning like this

Infinite yield possible on 'Workspace.Boss:WaitForChild(“Humanoid”)

As a newbie scripter, this is the weirdest script I’ve stumbled into, since after fixing the error and there goes another error.

1 Like

The problem is simple. When one humanoid dies the character get removed and one new comes the humanoid you tried to target is not the new one. Like if he got removed you have to find the new one again.

Ahh, I think I see my mistake. The “WaitForChild” is misbehaving with Heartbeat. I’ll fix that real quick.

As I understand it, Heartbeat calls the check 60 times a second, and the wait holds the checks all to be released at once. No need to hold onto them. Now if the Humanoid is not there, the code will ditch it!

And that’s coding for you! LOL, fix one thing, break something else.

“Fatal” errors stop the script. If there are multiple errors, the new ones don’t show up until you fix the old one. Those aren’t the ones you need to worry about. The errors you need to worry about are the ones that do exactly what you told them to do, but are still wrong.


if boss and boss.Health <= 0 then -- Error: Humanoid is not a valid member of Model "Workspace.Boss"

And yep, there goes another error. Just found out that Humanoid gets destroyed after killing the boss, doesn’t give every players a reward. Tried some wait() below the statement aka “if then”, so all players in the server will receive a reward before Boss’s Humanoid will be destroyed, but it didn’t fix.

That’s a weird bug. The function giving out rewards does not use humanoid. By any chance are the people who don’t get rewards “New” as in they aren’t on the leaderboard already?

There will be no leaderboard. I use this function so players could buy some in-game items. ( sorry for bad grammar btw )

OK, I will rephrase the question. Are the people not receiving cash currently at 0?

I made another script which every 60 seconds, everyone will get 10 points and it repeats. Maybe it will never affect the reward script.

Unless you throw the health check into the damage script (so that the reward is given out right before the boss dies and the humanoid is deleted), I’m out of ideas. I thought heartbeat would be fast enough. /shrug

.HealthChanged is LocalScript only
.Died is LocalScript only