Reading data with the userid

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)
1 Like

basically, you have to use Intvalues to store the cash in the text label not ordinary text labels

1 Like

so in the script i store the cash in the inbtvalues ?

yea hold on, i’ll help you do them

1 Like

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)

1 Like

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.

1 Like

Ok so :
I got this error : image

on the local script for the text label?

did you 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)

1 Like

ok its me … im so bad haha i found my error ! thx you a lot man ! <3

1 Like

just a little question, how can i increment value ?

Use :IncrementAsync()

Incrementing Data

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.

you mean increase value?