I’m making a simulator game and I’m trying to make a leaderstats script, normally it works but when making it this time it doesn’t work, I’ve tried using multiple methods
function onPlayerEntered(newPlayer)
wait(.5)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
local score = Instance.new("IntValue")
score.Name = "Xp"
score.Value = 0
score.Parent = stats
stats.Parent = newPlayer
end
game.Players.ChildAdded:connect(onPlayerEntered)
this method only makes the first Variable
and I’ve tried putting the leaderstats folder into replicated storage then cloning it into the player, but this doesn’t work either, not even a warning on the output to show me what went wrong
Just for your knowledge you shouldn’t use the ChildAdded event to detect when the player joins the game when you have the PlayerAdded event because the ChildAdded event will fire each time something is added to the PlayersService even if it is not a player. Using the PlayerAdded event is a lot more reliable for detecting when players join the game than ChildAdded.
You should also remove the wait you have at the beginning of your code because it is unnecessary and you should properly indent your code to improve readability. Here is what your code will look like with these changes:
function onPlayerEntered(newPlayer)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
local score = Instance.new("IntValue")
score.Name = "Xp"
score.Value = 0
score.Parent = stats
stats.Parent = newPlayer
end
game.Players.PlayerAdded:Connect(onPlayerEntered)
Note: connect is depreciated in favour of Connect to keep with the CamelCase Roblox uses for their API.