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