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

How do I loop this script so it dosen't stop when used?

This script works when the zombie named "Humanoid" dies and Money is given to the player. The script only works one, eg when I kill the zombie money is given, but when I kill the zombie again the script doesn't work.

I need this script to loop,

local Humanoid = game.Workspace.Humanoid

game:GetService('Players').PlayerAdded:Connect(function(player)
	    player.CharacterAdded:Connect(function(character)
	        Humanoid:WaitForChild("Zombie").Died:Connect(function()
	            player.RealMoney.Value = player.RealMoney.Value + 1000
	        end)
	    end)
	end)

1 answer

✓ Accepted answer

This is because you are using PlayerAdded, which won't happen again. It is a bad idea to put connect into a loop. Simply keep your died event out, and get the player using the touching event. For example, assuming no parts of the weapon are parented to one another, you could do this:

Humanoid:WaitForChild("Zombie").Touched:Connect(function(part)
	if Humanoid.Zombie.Health > 0 then
		return --Just do nothing, not sure if return will work over here
	else
		local char = part.Parent.Parent

		if char:FindFirstChild("Humanoid") then
			local player = char:GetPlayerFromCharacter()

			if player then
				player.RealMoney.Value = player.RealMoney.Value
			end
		end
	end
end)

Hope this helped.

2 comments

0

Is this for a local script in serverscriptsevice or not?

0

this is a zombie i think

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