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 run a code once every time a player respawns?

I would want to make a code that gives the player a PointLight every time they spawn. They will not receive any additional PointLights, just one. When I spawn, I get a PointLight, but when I respawn, I won't have any additional PointLights anymore. I also want to apply this to the other players. The script is normal and the parent of it is a spawn location. Here's the code:

local touched do
    touched = workspace.SpawnLocation.Touched:Connect(function(hit)
		local light = Instance.new('PointLight')
 		light.Parent = hit.Parent.Head
 		light.Brightness = 3
        touched:Disconnect()
    end)
end

How do I make it that every time you die, you still get the PointLight and the other players will still get the PointLight aswell?

2 answers

✓ Accepted answer

You can do this with the CharacterAdded event. This will run every time the linked a Character of a user is refreshed; meaning LoadCharacter is called, or they died.

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
        local Head = Character:WaitForChild("Head")
        local Light = Instance.new("PointLight"); Light.Parent = Head
        Light.Brightness = 3
    end)
end)

Use the Humanoid.Died event to check if a player’s health is equals to 0. To do this, simply write this piece of code and then put your script in the event:

local player = game.Players.LocalPlayer
local char = player.Character

char:FindFirstChild(“Humanoid”).Died:connect(function()
      --Code will run when player has died
end)

If you need a little more help, you can go to this link here:

https://developer.roblox.com/en-us/api-reference/event/Humanoid/Died

1 comment

0

Oh and sorry, Im on my iPad so those speech marks is different.

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