#Can someone explain why this data store doesn't save data?
16 messages · Page 1 of 1 (latest)
local dataStoreService = game:GetService("DataStoreService")
local clicksDataStore = dataStoreService:GetChildren("Clicks")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
local clicks = Instance.new("IntValue",leaderstats)
clicks.Name = "Clicks"
clicks.Value = 0
local playerUserId = "player_"..player.UserId
local clicksData
local success, errormessage = pcall(function()
clicksData = clicksDataStore:GetAsync(playerUserId)
end)
if success then
clicks.Value = clicksData
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "player_"..player.UserId
local clicksValue = player.leaderstats.Clicks.Value
local success, errormessage = pcall(function()
clicksDataStore:SetAsync(playerUserId, clicksValue)
end)
end)
game:BindToClose(function(player)
for _, Player in pairs(game.Players:GetPlayers()) do
local playerUserId = "player_"..Player.UserId
local clicksValue = player.leaderstats.Clicks.Value
local success, errormessage = pcall(function()
clicksDataStore:SetAsync(playerUserId, clicksValue)
end)
end
end)
I believe it has to do with your clicksDataStore variable, the way you saved it is wrong.
local clicksDataStore = dataStoreService:GetDataStore("Clicks")
You didn't GetDataStore, that's a fixed version of the variable
When a player leaves you still need to assign that new data to clicksData, for example:
local success, errormessage = pcall(function() clicksDataStore:SetAsync(playerUserId,clickValue)
end)
-- This is missing clicksData
so it would be
local success, errormessage = pcall(function()
clicksData = clicksDataStore:SetAsync(playerUserId,clickValue)
end)
@placid shale I'm off to sleep so if you still need help don't expect an answer till 12:00 PM EST
But yeah I think that's it. Fix your variable, and SetAsync properly and you should be good. If your "Clicks" GetDataStore has been tampered with already you may need to assign a new GetDataStore unless you know how to remove such data. But I doubt any of that needs to happen.
TYSM BRO ILL TEST IT WHEN I GET HOME :))