What the problem

Hello Developers,

I have a problem with my script.

local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("PlayerData")





local function onPlayerJoin(player)  -- Runs when players join

	local leaderstats = Instance.new("Folder")  --Sets up leaderstats folder

	leaderstats.Name = "leaderstats"

	leaderstats.Parent = player



	local Cash = Instance.new("IntValue") --Sets up value for leaderstats

	Cash.Name = "Cash"

	Cash.Parent = leaderstats
	
	
	
	local Rank = Instance.new("StringValue")
	Rank.Name = "Rank"
	Rank.Parent = leaderstats


	local playerUserId = "Player_" .. player.UserId  --Gets player ID

	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data

	if data then

		-- Data exists for this player

		Cash.Value = data
		Rank.Value = data

	else

		-- Data store is working, but no current data for this player

		Cash.Value = 0
		Rank.Value = "Guest"
		
	end

end



local function onPlayerExit(player)  --Runs when players exit



	local success, err = pcall(function()

		local playerUserId = "Player_" .. player.UserId

		playerData:SetAsync(playerUserId, player.leaderstats.Cash.Value, player.leaderstats.Rank.Value) --Saves player data

	end)



	if not success then

		warn('Could not save data!')

	end

end



game.Players.PlayerAdded:Connect(onPlayerJoin)

game.Players.PlayerRemoving:Connect(onPlayerExit)

In order to help, we need more information. What is the problem in particular? Any errors?

2 Likes

This part had an error. I don’t know what it is

What did the error say in the console?

Script ‘ServerScriptService.Script’, Line 34 - function onPlayerJoin - Studio - Script:34

That’s not the error, that’s just what tells you where it is, could you send an image of your output or copy the entire message?

20:12:46.570 502: API Services rejected request with error. HTTP 403 (Forbidden) - Server - Script:34
20:12:46.570 Stack Begin - Studio
20:12:46.570 Script ‘ServerScriptService.Script’, Line 34 - function onPlayerJoin - Studio - Script:34
20:12:46.570 Stack End - Studio
Oh I’m understanding what I need to do. I need to turn on API services.

playerData:SetAsync(playerUserId, player.leaderstats.Cash.Value, player.leaderstats.Rank.Value) --Saves player data

We do need more information on that GetAsync() issue, but I noticed an issue here. you need to package these into a table in order to properly save them. You cant pass them as extra arguments as it takes a value, not a tuple as the second argument to SetAsync().

Turn on Studio API services. It should be in the game settings. That should fix the issue.

Thanks for the help guys! I appreciate it!

1 Like