[THIS SCRIPT IS NOT MINE, I had permission to use it] Hello, I am trying to make this script autosave, but I do not understand how to do it. Whenever I leave the game the kills and deaths don’t save. I have been searching and searching but nothing seems to help. I hope you guys can!
Script:
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 local deaths = Instance.new(“IntValue”) deaths.Name = “Deaths” deaths.Parent = stats plr.CharacterAdded:connect(function(char) local humanoid repeat humanoid = char:FindFirstChild(“Humanoid”) wait() until humanoid humanoid.Died:connect(function() deaths.Value = deaths.Value + 1 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)
Please do not use Code Review to request for help accomplishing tasks, that is what the Scripting Support category is for. I have recategorised this post. Please do read category guidelines in the future before creating threads on the forum.
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
local deaths = Instance.new("IntValue")
deaths.Name = "Deaths"
deaths.Parent = stats
plr.CharacterAdded:connect(function(char)
local humanoid
repeat humanoid = char:FindFirstChild("Humanoid")
wait()
until humanoid humanoid.Died:connect(function()
deaths.Value = deaths.Value + 1
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)
Is studio API services turned on? It should be turned on only if you are testing data stores in studio, that’s it.
Please use code block for your code since your code is not a code, but rather a mess.
I’m not going to give you the full code, but a demonstration to help you figure out what to do.
local Data = game:GetService("DataStoreService"):GetDataStore("Data")
game.PlayersAdded:Connect(function(player)
local plr_data
plr_data = Data:GetAsync(player.UserId.."-key") -- GetAsync needs 1 argument and that is the key you are saving the data to.
end)
game.Players.PlayerRemoving:Connect(function(player)
data:SetAsync(player.UserId.."-key", -- value or data to save)
end)
Player Removing may not always fire on player leave. Therefore, it is recommended to use Game:BindToClose() instead.