This is my script:

local DataStoreService = game:GetService("DataStoreService")
local CashDataStore = DataStoreService:GetDataStore("CashData")
local GemDataStore = DataStoreService:GetDataStore("GemData")

game.Players.PlayerAdded:Connect(function(plr)
	
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"
	
	local Cash = Instance.new("IntValue", leaderstats)
	Cash.Name = "Cash"
	Cash.Value = CashDataStore:GetAsync(plr.UserId)
	
	local Gems = Instance.new("IntValue", leaderstats)
	Gems.Name = "Gems"
	Gems.Value = GemDataStore:GetAsync(plr.UserId)
	
	Cash.Changed:Connect(function()
		
		CashDataStore:SetAsync(plr.UserId, Cash.Value)
		
	end)
	
	Gems.Changed:Connect(function()

		GemDataStore:SetAsync(plr.UserId, Gems.Value)

	end)
	
	
end)

When I go in-game the data doesn't save.

Is there anything I'm doing wrong?

I have API Services on.

3 comments

0

Do you mind using player removing event instead of a changed event?

0

shall i replace the changed events with player removing events?

0

well yes, its more optimized, you need to use pcalls for it, watch alvin blox tutorial on it