Datastore giving other people other players stats

So I use this simple little script that iv been using for a while and never thought to update it, but I need to now because its giving other players my stats. How do I fix this?

local DoAutoSave = true
local AutoSaveInterval = 120

local DataStore = game:GetService("DataStoreService"):GetDataStore('HatSave')

local currentsave
local stats
local currentstat
local tabletosave

game.Players.PlayerAdded:Connect(function(player)
	currentsave = DataStore:GetAsync(player.UserId)
	stats = player:WaitForChild("stats")
	if currentsave then
	for i = 1,#currentsave do
		currentstat = stats:WaitForChild(currentsave[i]['Name'])
		currentstat.Value = currentsave[i]['Value']
	end
	print('Loaded stats')
	else
		print('Player is new and yes do be')
	end
	if DoAutoSave then
		spawn(function()
			repeat
				wait(AutoSaveInterval)
				if player then
					tabletosave = {}
					for _,v in pairs(player.stats:GetChildren()) do
						tabletosave[#tabletosave+1] = {Name = v.Name,Value = v.Value}
					end
					DataStore:SetAsync(player.UserId,tabletosave)
				else
					print('Player is not there, he dipped"')
				end
			until not player
		end)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	if player then
		tabletosave = {}
		for _,v in pairs(player.stats:GetChildren()) do
			tabletosave[#tabletosave+1] = {Name = v.Name,Value = v.Value}
		end
		DataStore:SetAsync(player.UserId,tabletosave)
	else
		print("The man is not there")
	end
end)

I’ve created a tutorial on this:

Saving multiple stats with 1 datastore.
Instead of instancing new ints then do this:

local TableStats = {"Cash", "Gems", "Gold"}

for i,v in pairs(TableStats) do
    local Instance = instance.new("IntValue", plr) -- Replace plr to where you want these values to go.
    Instance.Name = i
end

I have never really worked with datastore, so im very confused as to what to do.

The video i gave you should help you with learning.

Also adding a new Table value is simply like this:

local Table = {} -- Make sure to make it a table.

Table["NewInstance"] = 100 -- this makes a new value inside the table called NewInstance with a set value of 100.

Yeah, before I had the script get me all the stats in pairs, but now i have to individually put them in here?

Can you elaborate so i fully understand?

So in your script, it says

	local stats = Instance.new("Folder", plr)
	stats.Name = "stats"
	
	local Cash = Instance.new("IntValue", stats)
	Cash.Name = "Cash"
	local Gems = Instance.new("IntValue", stats)
	Gems.Name = "Gems"

but in my old script, it just got the stats children in pairs, and went from there. So I didnt have to.

Ah i’m sorry. I’m have only recently started YouTube so i ain’t got many tutorials.

You can keep the Stats folder as that will be where the stats will be stored so it’s organized.

What you can do is do this for stats:

local TableStats = {"Cash", "Gold"} -- Create or add new stats. Order isn't required.

and then you can create them like this:

for i,v in pairs(TableStats) do
    local Inst = instance.new("IntValue", Stats) -- Stats is the Folder to place them in.
    Inst.Name = i
end -- This would create all 2 of the tables int values in Stats.

Your fine, but what I meant by this is like, if you look at the original script, it just got the stats without me having to put them in a table.

Your script is messy and not at all reliable. No offense.
So i can’t understand it fully. However i do notice it’s not how you properly create Data.

The way i showed you will be better and more reliable atleast. And this is pretty much the same as yours i think.

Alright ill try it. I hope it works better.

If you don’t want to do it then i also have created a Plugin for easy creating data.
https://www.roblox.com/library/4503503893/Customizable-Datastore

But i do recommend you learning how data stores work as shown in my Video for proper and better results.