I'm trying to make the script wait until when the player's character loads to work, I've tried to add code here but I don't know what I'm doing wrong, I may be missing an end but I don't know where to place it.
local datastore = game:GetService("DataStoreService"):GetDataStore("DataStore15")
game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(function(char)
while player:HasAppearanceLoaded() == false do
wait()
local leaderstats = Instance.new("IntValue")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local xd = Instance.new("IntValue")
xd.Name = "example"
xd.Parent = leaderstats
xd.Value = 1
local key = "user-" .. player.userId
local storeditems = datastore:GetAsync(key)
if storeditems then
xd.Value = storeditems[1]
else
local items = {xd.Value}
datastore:SetAsync(key, items)
end
end)
game.Players.PlayerRemoving:connect(function(player)
local items = {player.leaderstats.example.Value}
local key = "user-" .. player.userId
datastore:SetAsync(key, items)
end)

2 comments
Thank you for replying!! Can you please show me what you mean by using the event instead of what I have?
Ok i think I may have figured it out