So I've been trying for quite a while now to make a Kills leaderstats that saves with DataStores on exit but I keep getting error after error. Finally, when I thought I had solved it, I got another error and I can't fix this one. I just want one of you to either revise my script so I don't get any errors or give me a better Kills script that also saves on exit.
Here is my script below:
local DataStoreService = game:GetService("DataStoreService") local DataStore = DataStoreService:GetDataStore("KillStats")
game.Players.PlayerAdded:Connect(function(plr)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = plr
local kills = Instance.new("IntValue")
kills.Name = "Kills"
kills.Parent = stats
plr.CharacterAdded:Connect(function(char)
local humanoid
repeat
humanoid = char:FindFirstChild("Humanoid")
wait()
until humanoid
humanoid.Died:connect(function()
local tag = humanoid:FindFirstChild("creator")
if tag then
local killer = tag.Value
if killer then
killer.leaderstats.Kills.Value = killer.leaderstats.Kills.Value + 1
end
end
end)
end)
end)
local Data = DataStore:GetAsync(Player.UserId)
if Data then
kills.Value = Data
end game.Players.PlayerRemoving:Connect(function(Player) DataStore:SetAsync(Player.UserId, Player.leaderstats.Kills.Value) end)
Thanks!

3 comments
the part where your trying to load the data actually put it into the 'playeradded' event function
also change the 'Player.UserId' to 'plr.UserId' in the loading data parameter
Please reformat your code. It looks like you've snipped part of the code so include the whole script.