Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I make this script loop, it only works once, Should I use a while loop?

This script is a server script in the zombie This script is for when the zombie dies then the player is given money, it is a 1 man server. The script works only once, e.g I kill the zombie once then when it respawns it dosen't work anymore.

local Zombie = game.Workspace.Zombie1



game:GetService('Players').PlayerAdded:Connect(function(player)
	Zombie:WaitForChild("Humanoid").Died:Connect(function()
		local boost = player:FindFirstChild("CoinBoost")
		player.RealMoney.Value = player.RealMoney.Value + boost.Value + 1000
	end)
	end)

2 comments

0

Is the zombie name the same after it respawn ?

0

Yes

2 answers

✓ Accepted answer

You do not make a new connection when the zombie re spawns. You are going about this problem the wrong way. Try something like this:

local function isZombie(model)
	return model and model:IsA("Model") and model.Name == "Zombie1"
end

game:GetService('Players').PlayerAdded:Connect(function(player)	
	local function checkChild(child)
		if isZombie(child) then
			child:WaitForChild("Humanoid").Died:Connect(function()
		        local boost = player:FindFirstChild("CoinBoost")
		        player.RealMoney.Value = player.RealMoney.Value + boost.Value + 1000
	    	end)
		end
	end
	
	for _, child in pairs(workspace:GetChildren()) do
		checkChild(child)
	end

	workspace.ChildAdded:Connect(checkChild)
end)

2 comments

0

The same thing happend the script only worked once, is it ment to be in server script service or a local script?

0

Thx it works now I put it in serverscipt service Thx soo much, I have struggled on this script for ages

The script looks fine and all at a first glance, i think when the zombie dies it creates a new copy of itself and removes the old zombie, when the variable is still located at the old zombie, try my solution

Use this:

local Zombie = game.Workspace.Zombie1

game:GetService('Players').PlayerAdded:Connect(function(player)
    Zombie:WaitForChild("Humanoid").Died:Connect(function()
        local boost = player:FindFirstChild("CoinBoost")
        player.RealMoney.Value = player.RealMoney.Value + boost.Value + 1000
    end)

	game.Workspace.ChildAdded:Connect(function(char)
		if char:FindFirstChild("Humanoid") and char.Name == "Zombie1" then
			Zombie = char
		end
	end)
end)

6 comments

0

let me know if it works and if any errors occur

0

Ill try it, your explination makes sense now

0

It may not work or not let me know ill just think about something else then

0

The same problem happens, I only get the cash once

0

Use the solution the other guy gave you, his script seems more probable to work.

0

Thx

This discussion is preserved as it was. New answers, comments, and votes are closed.