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

How do I give an item to a specific player only when the player joins and respawns?

Asked by 8 years ago

Whenever a player with a certain name joins, a script must execute and give the player a weapon and place it in the Backpack. But since it is not put in 'StarterPack' because it is only for one player, the weapon will be gone when the player respawns. So it will have to be given both when the player joins and respawns.

I have a script that tests if the player joins but I don't know what I have to do for it to execute when the player respawns:

01sword = game.ServerStorage.GreenSword
02local swordCopy = sword:Clone()
03 
04while true do
05    local player = game.Players:FindFirstChild("Inscendio")
06 
07    if player then
08        swordCopy.Parent = player.Backpack
09    return end
10 
11    wait(1)
12end

So I really need help with giving the weapon when the player respawns.

1 answer

Log in to vote
2
Answered by 8 years ago
Edited 8 years ago

It is not a good idea to use the players name as this can change, I would use UserId. We can use the CharacterAdded event which fires when the players character is added to the game.

This is a very basic example using the players name.

01-- get the services
02local plrSrv = game:GetService('Players')
03local sword = game:GetService('ServerStorage'):WaitForChild('sword')
04 
05-- we are using a table so we can quickly check for the players name if not found it will be nil
06-- this also allows us to quickly add new players without changing much code 
07 
08-- it would be best to use the players UserId but for this example I will use the player name
09local allowedList = {
10    ['Player1'] = true,
11    ['Player2'] = true
12}
13 
14-- connect a player added event
15plrSrv.PlayerAdded:connect(function(plr)
View all 23 lines...

I hope this helps, please comment if you do not understand how / why this code works.

0
Thanks for helping. It was useful but my problem for now is that how do I make it so that it also gives when the player respawns. Inscendio 42 — 8y
0
Too many comments, makes it hard to read. Async_io 908 — 8y
2
CharacterAdded "Fired when a player's character spawns or respawns" so it will add the tool each time the respawn. User#5423 17 — 8y
1
I will do that automatically Inscendio. Async_io 908 — 8y
0
Oh okay thanks Inscendio 42 — 8y
Ad

Answer this question