I’ve been working on this forever and even though I’ve done everything right. My dataStore still wont work. The goal is that everytime you drink a bottle of water your “Drinks” count goes up by one.
Sometimes it will save the Data if I change it manually, but if I use a tool that changes it, it never saves
I’ve looked all over the forums and tried multiple solutions and non of them worked. Can someone please help me
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local RunService = game:GetService("RunService")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local HydrationFolder = Instance.new("Folder")
HydrationFolder.Name = "HydrationFolder"
HydrationFolder.Parent = player
local Drinks = Instance.new("IntValue")
Drinks.Name = "Drinks"
Drinks.Parent = leaderstats
local Ammo = Instance.new("IntValue")
Ammo.Name = "Ammo"
Ammo.Parent = player
local MaxHydration = Instance.new("IntValue")
MaxHydration.Name = "MaxHydration"
MaxHydration.Parent = HydrationFolder
MaxHydration.Value = 100
local pUserId = ""..player.UserId
local dData
local success, errormessage = pcall(function()
dData = myDataStore:GetAsync(pUserId)
end)
if success then
if dData ~= nil then
Drinks.Value = dData
end
print("Player data successfully loaded.")
else
print("There was an error when saving data.")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local pUserId = ""..player.UserId
local dData = player.leaderstats.Drinks.Value
local success, errormessage = pcall(function()
myDataStore:UpdateAsync(pUserId,function(oldValue)
if oldValue ~= nil then
print("oldValue: "..oldValue)
end
print("data: "..dData)
return dData
end)
end)
if success then
print("Player Data successfully saved.")
else
print("There was an error when saving data.")
warn(errormessage)
end
end)
game:BindToClose(function()
if not RunService:IsStudio() then
local players = game.Players:GetPlayers()
for _, player in pairs(players) do
player:Kick("Server is shutting down: Please rejoin, your data will be saved.")
end
while true do
wait()
end
else
wait(2)
end
end)