Hello !
I search in the documentation of roblox for reading data, but im blocked when i need to find the userId of the player … can you help me ?
This is my script in the serverScriptService
local DataStoreService = game:GetService("DataStoreService")
local goldStore = DataStoreService:GetDataStore("PlayerGold")
local player = game:GetService("Players")
local playerMoneyDefault = 50
local function onPlayerAdded(player)
local playerKey = player.UserId
print(playerKey)
local success, err = pcall(function()
goldStore:SetAsync(playerKey, playerMoneyDefault)
end)
if success then
print("Success!")
end
end
player.PlayerAdded:Connect(onPlayerAdded)
And this is my script in my text label ( i want to read the data for after display it on the text label )
local DataStoreService = game:GetService("DataStoreService")
local goldStore = DataStoreService:GetDataStore("PlayerGold")
local player = game:GetService("Players").LocalPlayer
local moneyTEXT = script.Parent
local success, currentmoney = pcall(function()
return goldStore:GetAsync(player.UserId)
end)
print("Current Experience:", currentmoney)
so first, Add An IntValue into Workspace and name it GoldValue
and then change the local script in your text Label to
local label = script.Parent
local player = game.Players.LocalPlayer
local gold = player.PlayerGoldFolder.GoldValue
label.Text = tostring(gold.Value)
And Lastly, Change the script in serverscriptservice to
local DataStoreService = game:GetService(“DataStoreService”)
local DataStore = DataStoreService:GetDataStore(“MoneyStats”)
game.Players.PlayerAdded:Connect(function(Player)
local gold = Instance.new(“Folder”, Player)
gold.Name = “PlayerGoldFolder”
local goldval = workspace.GoldValue
goldval.Name = "GoldValue"
goldval.Parent = gold
local Data = DataStore:GetAsync(Player.UserId)
if Data then
goldval.Value = Data.Cash -- Change "Money" with your currency.
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, {
[“Cash”] = Player.PlayerGoldFolder.GoldValue.Value; – Change “Money” with your currency.
})
end)
Also make sure you have api services enabled in your game! and if you want the player default money to be 50, change the value of the intvalue to 50, it makes it the default gold.
IncrementAsync() changes a numerical value in a data store. This function requires the key name of the entry and a number indicating how much to change the value.