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

How to see when the local player has spawned in the game?

Asked by 4 years ago
Edited 4 years ago

So basically I need something that would run this script when the player respawns

game.ReplicatedStorage.RemoteEvent:FireServer()

When the player joins the game this RemoteEvent gives him a tool, but when he dies the script doesn't run again and the player doesn't get his tool.

2 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

you would just listen for player.CharacterAdded event...

local function onCharacterLoaded(character)
    --code to give tool
end

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(onCharacterLoaded)
end)


the above has to be in a server script btw.

if you really want the event at which the player loads into the game to be client side, then you have one or both of the following examples to use..

In LocalScript

local function onCharacterLoaded(character)
    --code to give tool
    if (player.Character) then
        --give tool
    end
end

example 1:

local player =  game.Players.LocalPlayer
player:GetPropertyChangedSignal("Character"):Connect(OnCharacterLoaded);

example 2: 1). put the a locascript in a ScreenGui object, and change screenGui.ResetOnSpawn to true

2). Parent the ScreenGui to StarterGui;

3). put the following code in the localscript:

player = game.Players.LocalPlayer

--code to give tool
game.ReplicatedStorage.RemoteEvent:FireServer();

**please not: the 2 examples aren't as efficient as using a server to listen for these things, and I really don't recommend using them, since they aren't best practice

so use the first snip of code to do the work **

Ad
Log in to vote
1
Answered by 4 years ago

Adding on to what @Arsubia12 said, another way to do this is to add a LocalScript or ServerScript into StarterCharacterScripts which is under StarterPlayer. All scripts in here are cloned and parented to the Character automatically when they spawn. This way you can just have that 1 line in there.

game.ReplicatedStorage.RemoteEvent:FireServer()

Now if you are using a ServerScript, you can literally remove the remote event and just put whatever you want to run in that script. If you're worried about how to get the LocalPlayer this is how:

local Players = game:GetService("Players")
local Character = script.Parent -- this is because it's cloned into the character
local Player = Players:GetPlayerFromCharacter(Character)

This is just an easier / lazier method.

I hope this helped and good luck.

0
good answer User#23252 26 — 4y

Answer this question