I was making a script that gives you a random power once you join, here is the code:
01 | local Players = game:GetService( "Players" ) |
02 |
03 |
04 | function onPlayerAdded(player) |
05 |
06 | local Quirks = game.ServerStorage.Quirks:GetChildren() |
07 | local RandomizeQuirks = math.random( 1 , #Quirks) |
08 | local QuirksChosen = Quirks [ RandomizeQuirks ] |
09 | print (QuirksChosen) |
10 | local quirk = QuirksChosen:Clone() |
11 | local backpack = player:WaitForChild( "Backpack" ) |
12 | quirk.Parent = backpack |
13 |
14 | end |
15 |
16 | Players.PlayerAdded:connect(onPlayerAdded) |
17 |
18 | for _,player in pairs (Players:GetPlayers()) do |
19 | onPlayerAdded(player) |
20 | end |
But everytime the player resets or dies, they lose the power. Any way i can fix that error?
You can implement the CharacterAdded
event into your code to make the power be added into their inventory whenever they spawn :)
Btw - the generic for loop is redundant since there are no players in your game at the start of the server.
01 | game.Players.PlayerAdded:Connect( function (player) |
02 | player.CharacterAdded:Connect( function () |
03 | local Quirks = game.ServerStorage.Quirks:GetChildren() |
04 | local RandomizeQuirks = math.random( 1 , #Quirks) |
05 | local QuirksChosen = Quirks [ RandomizeQuirks ] |
06 | print (QuirksChosen) |
07 | local quirk = QuirksChosen:Clone() |
08 | quirk.Parent = player:WaitForChild( "Backpack" ) |
09 | end ) |
10 | end ) |