So I was trying to save a player’s wins with DataStoreService, but even changing my wins count to 100 still prints Derpee_Kirbee's data has been saved as 0 Wins! Here is the script:
Script
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local wins = Instance.new("IntValue",leaderstats)
wins.Name = "Wins"
while wait(10) do
local success, errorMessage = pcall(function()
myDataStore:SetAsync(player.UserId, player.Name .. " " .. player.leaderstats.Wins.Value)
end)
if success then
print(player.Name .. "'s data has been saved as " .. player.leaderstats.Wins.Value .. " Wins!")
else
print(player.Name .. "'s data did not save!")
warn(errorMessage)
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errorMessage = pcall(function()
myDataStore:SetAsync(player.UserId, player.Name .. " " .. player.leaderstats.Wins.Value)
end)
if success then
print(player.Name .. "'s data has been saved as " .. player.leaderstats.Wins.Value .. " Wins!")
else
print(player.Name .. "'s data did not save!")
warn(errorMessage)
end
end)
@Dolphin_Worm I don’t entirely understand what you mean. I am changing the value and waiting for it to print again, but it’s still printing the same thing.
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local wins = Instance.new("IntValue",leaderstats)
wins.Name = "Wins"
--You should add a pcall here to :GetAsync() the player data so when you :SetAsync() the player data it doesn't stay 0.
while wait(10) do
local success, errorMessage = pcall(function()
myDataStore:SetAsync(player.UserId, player.Name .. " " .. player.leaderstats.Wins.Value)
end)
if success then
print(player.Name .. "'s data has been saved as " .. player.leaderstats.Wins.Value .. " Wins!")
else
print(player.Name .. "'s data did not save!")
warn(errorMessage)
end
end
Alright thanks a lot I’m sort of new to DataStores I will try that out
EDIT: @Dolphin_Worm I’m still sort of confused. Why would I need to GetAsync? Why wouldn’t it just get the current leaderstats value instead of an old one?