I was making a script that gives you a random power once you join, here is the code:
local Players = game:GetService("Players") function onPlayerAdded(player) local Quirks = game.ServerStorage.Quirks:GetChildren() local RandomizeQuirks = math.random(1, #Quirks) local QuirksChosen = Quirks[RandomizeQuirks] print(QuirksChosen) local quirk = QuirksChosen:Clone() local backpack = player:WaitForChild("Backpack") quirk.Parent = backpack end Players.PlayerAdded:connect(onPlayerAdded) for _,player in pairs(Players:GetPlayers()) do onPlayerAdded(player) 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.
game.Players.PlayerAdded:Connect(function(player) player.CharacterAdded:Connect(function() local Quirks = game.ServerStorage.Quirks:GetChildren() local RandomizeQuirks = math.random(1, #Quirks) local QuirksChosen = Quirks[RandomizeQuirks] print(QuirksChosen) local quirk = QuirksChosen:Clone() quirk.Parent = player:WaitForChild("Backpack") end) end)