when i die in my game, for some odd reason, my data resets, i dont know how.
script:
local DataStoreService = game:GetService("DataStoreService")
local Data = DataStoreService:GetDataStore("Info")
local function saveData(player) -- The functions that saves data
local tableToSave = {
player.leaderstats.Gold.Value; -- First value from the table
player.leaderstats.WalkSpeed.Value; -- Second value from the table
player.PlayerGui.ScreenGui.shop.Speed.max;
player.PlayerGui.ScreenGui.shop.Speed.speedprice
}
local success, err = pcall(function()
Data:SetAsync(player.UserId, tableToSave) -- Save the data with the player UserId, and the table we wanna save
end)
if success then -- If the data has been saved
print("Data has been saved!")
else -- Else if the save failed
print("Data hasn't been saved!")
warn(err)
end
end
game.Players.PlayerAdded:Connect(function(p)
p.CharacterAdded:Connect(function()
-- Currency
local Leaderstats = Instance.new("Folder", p)
Leaderstats.Name = "leaderstats"
local gold = Instance.new("IntValue", Leaderstats)
local walkspeed = Instance.new("IntValue", Leaderstats)
gold.Name = "Gold"
walkspeed.Name = "WalkSpeed"
-- DataBase
local data
local success, err = pcall(function()
data = Data:GetAsync(p.UserId)
end)
if success then
walkspeed.Value = data[2]
gold.Value = data[1]
else
print("Couldn't load!")
end
--p.CharacterAdded:Connect(function()
-- p.Character.Humanoid.Died:Connect(function()
-- p.Team = game.Teams.spectator
-- end)
--end)
while wait() do
p.Character.Humanoid.WalkSpeed = walkspeed.Value+16
end
gold:GetPropertyChangedSignal("Value"):Connect(function()
saveData(p)
end)
walkspeed:GetPropertyChangedSignal("Value"):Connect(function()
saveData(p)
end)
end)
end)
game.Players.PlayerRemoving:Connect(function(player) -- When a player leaves
local success, err = pcall(function()
saveData(player) -- Save the data
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end)
game:BindToClose(function() -- When the server shuts down
for _, player in pairs(game.Players:GetPlayers()) do -- Loop through all the players
local success, err = pcall(function()
saveData(player) -- Save the data
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end
end)