Trying To Make This Leaderstats Script Autosave

[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)

1 Like

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.

1 Like

Can you please put you code in a codeblock whenever you post?
How am I supposed to help you when I see this?


This is the code for other people to help

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)

Also when i put that in studio, all the " were ``

1 Like

If you can format the script correctly, you will find the help you need. Format it correctly so that people may help your problem.

1 Like

I’m so sorry for getting this all in a mess guys. I was just, really confused.

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.

1 Like