Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How do I run a code once every time a player respawns?

Asked by 4 years ago

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

Log in to vote
1
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

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)
Ad
Log in to vote
0
Answered by 4 years ago

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

0
Oh and sorry, Im on my iPad so those speech marks is different. FrontsoldierYT 129 — 4y

Answer this question