also setting data within the changed event. Wouldn’t be a correct usage of the datastores because you get a set budget and when you surpass it the requests get queried
This Has DataStoring In Tables So no Issues should Occur.
I Tried explaining in the Script Itself.
Make Sure this Is A “Script” In “ServerScriptService”
Hope i could Help
local DataStoreService = game:GetService("DataStoreService")
local GameStore = DataStoreService:GetDataStore("playerData")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
--
Players.PlayerAdded:Connect(function(Player) -- When Player Joins
local newFolder = Instance.new("Folder") -- Make a New Folder
newFolder.Parent = Player -- Parent it to Player
newFolder.Name = "leaderstats" -- Call it Leaderstats
local berriesInt = Instance.new("IntValue") -- Add A Integer Value
berriesInt.Value = 0 -- Default Value will be 0
berriesInt.Parent = newFolder -- Parent it to the Folder
berriesInt.Name = "Berries" -- Call it Berries
local orangeInt = Instance.new("IntValue") -- Add A Integer Value
orangeInt.Value = 0 -- Default Value will be 0
orangeInt.Parent = newFolder -- Parent it to the Folder
orangeInt.Name = "Oranges" -- Call it Oranges
local oldData -- Blank Var
local foundData,newPlr = pcall(function() -- Wrap in Pcall so Script Does not Break
oldData = GameStore:GetAsync(Player.UserId) -- Check for Data
end)
if foundData and oldData then -- If Data
berriesInt.Value = oldData.Berries -- Set Data Berries
orangeInt.Value = oldData.Oranges -- Set Data Oranges
end
end)
Players.PlayerRemoving:Connect(function(Player) -- When Player Leaves
local savedData,didNot = pcall(function() -- Wrap in Pcall
GameStore:SetAsync(Player.UserId,{ -- Key Is Player's UserId and Data is Stored in Table
Berries = Player.leaderstats.Berries.Value, -- Berries is Stored as The Berries Int
Oranges = Player.leaderstats.Oranges.Value, -- Oranges is Stored as The Oranges Int
})
end)
if not savedData then -- If An Error
warn(didNot) -- Warn the Error in Output
end
end)
game:BindToClose(function() -- When Server Is Closing
if RunService:IsStudio() then -- If the Test is in ROBLOX Studio
wait(1) -- Wait 1 Second to Save Data from Previous Function
else -- Else
for _, Player in pairs(Players) do -- Loop through All Players
local UserId = Player.UserId -- Get UserId
local savedData,didNot = pcall(function() -- Wrap In Pcall
GameStore:SetAsync(UserId,{ -- Set the Key to Player's UserId and Store In Tables
Berries = Player.leaderstats.Berries.Value, -- Berries is Stored as The Berries Int
Oranges = Player.leaderstats.Oranges.Value, -- Oranges is Stored as The Oranges Int
})
end)
if not savedData then -- If An Error
warn(didNot) -- Warn the Error in Output
end
end
end
end)