Datastore script not working help

So I was watching a data store tutorial and copied off the scripts, but it isn’t working when I’m done the script, it doesn’t say anything wrong in the output and my api is on, but it just isn’t working, so can anyone figure out why my script isn’t working?

local DataStoreService = game:GetService(“DataStoreService”)
local Datastore = DataStoreService:GetDataStore(“Datastore”) – creates datat store

game.Players.PlayerAdded:Connect(function(player)

local leaderstats = Instance.new("Folder") -- make leaderstat
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

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

local playerUserId = "Player_"..player.UserId

-- 	Load Data

local data
local success, errormessage = pcall(function()
	data = Datastore:GetAsync(playerUserId)
end)


if success then
	Cash.Value = data
	-- set out data equal to current cash
end

end)

game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = “Player_”…player.UserId

local data = player.leaderstats.Cash.Value

local success, errormessage = pcall(function()
	Datastore:SetAsync(playerUserId, data)
end)

if success then
	print("SUCCESS")
else
	print("ERROR!")
	warn(errormessage)
end

end)

1 Like

If you are testing it in studio, it will not work, as testing roblox studio and leaving does not count as PlayerRemoving.

What is the point of using pcalls if you are not going to catch errors?

No, it can and does work in studio for me. You just need to put API services on.

I’m guessing the problem is in the PlayerAdded one, so you’re using a pcall() and you’re not printing the error (if you are facing one at all) overall this looks fine to me. Also I didn’t see you mention that you published your game after turning on API services, so I’d suggest you do that.