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:
01 | sword = game.ServerStorage.GreenSword |
02 | local swordCopy = sword:Clone() |
03 |
04 | while 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 ) |
12 | end |
So I really need help with giving the weapon when the player respawns.
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 |
02 | local plrSrv = game:GetService( 'Players' ) |
03 | local 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 |
09 | local allowedList = { |
10 | [ 'Player1' ] = true , |
11 | [ 'Player2' ] = true |
12 | } |
13 |
14 | -- connect a player added event |
15 | plrSrv.PlayerAdded:connect( function (plr) |
I hope this helps, please comment if you do not understand how / why this code works.