Datastores not working!

local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("PlayerData")





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

	local leaderstats = Instance.new('Folder')
	leaderstats.Name = 'Money_Stats'
	leaderstats.Parent = player

	local money = Instance.new('IntValue')
	money.Name = 'Money'
	money.Value = 000
	money.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

		money.Value = data

	else

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

		money.Value = 0

	end

end



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



	local success, err = pcall(function()

		local playerUserId = "Player_" .. player.UserId

		playerData:SetAsync(playerUserId, player.Money_Stats.Money.Value) --Saves player data

	end)



	if not success then

		warn('Could not save data!')

	end

end



game.Players.PlayerAdded:Connect(onPlayerJoin)

game.Players.Player
Removing:Connect(onPlayerExit)

Why isn’t my script working

1 Like

do you have API Services enabled?
also are you changing the money locally?
and do you get any errors in the output?

  1. Yes

  2. ??? Not Sure what you mean Money is done by a Remote_Event so money is added by Server script

And that script I showed is a server script in serverscriptstorage

  1. No

If their are any issues

try to add a print in if data then & else statement and check if it prints

ok at the player added??

or the playing removiimg

you already have in the player removing.

	if data then

		print("Yes")

		money.Value = data

	else

		print("No")

		money.Value = 0

	end

the print shows in the Player Added

which print?
like in the if statement or in the else?

local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("PlayerData")





local function onPlayerJoin(player)  
	*print("Working Ok Bro!")*


-- Runs when players join

	local leaderstats = Instance.new('Folder')
	leaderstats.Name = 'Money_Stats'
	leaderstats.Parent = player

	local money = Instance.new('IntValue')
	money.Name = 'Money'
	money.Value = 000
	money.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

		money.Value = data

	else

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

		money.Value = 0

	end

end



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



	local success, err = pcall(function()

		local playerUserId = "Player_" .. player.UserId

		playerData:SetAsync(playerUserId, player.Money_Stats.Money.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)

not in there, in the spots where i showed in here

il try it now give me 1 minuite

does it have to be played im studio or main game?

as I have tried both

both would work [Chars Limits]

It Shows

“Yes” For that print

so what could be whrong?

change print("Yes") to print(data)

I think that problem that you are having is that it is saving but the value is 0 for that data

now it only prints 0 but don’t it only save in mainstream roblox

I don’t think there is a problem since data is printing 0 which is money.Value

so why don’t it save?

This is now so puzzeling

Maybe it is because when the player leaves it does not have enough time to save.

Also are you changing money.Value from anywhere?