You can write your topic however you want, but you need to answer these questions:
I want to make player who join the game for the first time get 10 tokens,
I tried to change tokens.Value to 10, but its still not working, can someone please help? I really appreciate that if you answer me! Thanks!
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local RunService = game:GetService("RunService")
local dataStore = DataStoreService:GetDataStore("Official")
local function waitForRequestBudget(requestType)
local currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)
while currentBudget< 1 do
currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)
task.wait(5)
end
end
local function setupPlayerData(player: player)
local userID = player.UserId
local key = "Player_"..userID
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local tokens = Instance.new("IntValue", leaderstats)
tokens.Name = "Tokens"
tokens.Value = 10
local shards = Instance.new("IntValue", leaderstats)
shards.Name = "Shards"
shards.Value = 0
local success, returnValue
repeat
waitForRequestBudget(Enum.DataStoreRequestType.GetAsync)
success, returnValue = pcall(dataStore.GetAsync, dataStore, key)
until success or not Players:FindFirstChild(player.Name)
if success then
if returnValue == nil then
returnValue = {
Tokens = 10,
Shards = 0
}
end
print(returnValue)
tokens.Value = if returnValue.Tokens ~= nil then returnValue.Tokens else 0
shards.Value = if returnValue.Shards ~= nil then returnValue.Shards else 0
else
player:Kick("There was an error loading your Data! Roblox's DataStore might be down, try again later, or contact us through our Group!")
print(player.Name.."Data loading ERROR!!")
end
end
local function save(player)
local userID = player.UserId
local key = "Player_"..userID
local tokens = player.leaderstats.Tokens.Value
local shards = player.leaderstats.Shards.Value
local dataTable ={
Tokens = tokens,
Shards = shards
}
print(dataTable)
local success, returnValue
repeat
waitForRequestBudget(Enum.DataStoreRequestType.UpdateAsync)
success, returnValue = pcall(dataStore.UpdateAsync, dataStore, key, function()
return dataTable
end)
until success
if success then
print("Data Saved!")
else
print("Data Saving ERROR!!")
end
end
local function onShutdown()
if RunService:IsStudio() then
task.wait(2)
else
local finished = Instance.new("BindableEvent")
local allPlayer = Players:GetPlayers()
local leftPlayers = #allPlayer
for _, player in ipairs(allPlayer) do
coroutine.wrap(function()
save(player)
leftPlayers -= 1
if leftPlayers == 0 then
finished:Fire()
end
end)()
end
finished.Event:Wait()
end
end
for _, player in pairs(Players:GetPlayers()) do
coroutine.wrap(setupPlayerData)(player)
end
Players.PlayerAdded:Connect(setupPlayerData)
Players.PlayerRemoving:Connect(save)
game:BindToClose(onShutdown)
while true do
task.wait(300)
for _, player in pairs(Players:GetPlayers()) do
coroutine.wrap(save)(player)
end
end