Why is my leaderstats not saving when I edit the value

So I’m working on a system that gives you a random amount of money for ‘working’ every once in a while. Currently, I’ve got everything set up but every time when I call the paycheck function, it won’t save the data. I’ve already tried saving without the paycheck function, which worked fine.
Any tips?

Script that handles paychecks:

-- References 
local TweenService = game:GetService("TweenService")

local player
local userId
local oldMoney
local amount

-- Info for tweens

local TweenInfo1 = TweenInfo.new(3)
local goals1 = {}
goals1.Position = UDim2.new(0.3, 0,0.05, 0)

local TweenInfo2 = TweenInfo.new(3)
local goals2 = {}
goals2.Position = UDim2.new(0.3, 0,-0.5, 0)


-- Events

game.Players.PlayerAdded:Connect(function(plr)
	player = plr
	userId = plr.UserId
end)

-- Functions
function addMoney()
	local player = game.Players:GetPlayerByUserId(userId)
	amount = math.random(10,100)
	player.leaderstats.Money.Value = player.leaderstats.Money.Value + amount
end

local function showPaycheck()
	-- References
	local player = game.Players:GetPlayerByUserId(userId)

	local screenGui = player.PlayerGui:WaitForChild("Paycheck")
	local Frame = screenGui:WaitForChild("Frame")
	local Amount = Frame:WaitForChild("Amount")
	local Total = Frame:WaitForChild("Total")
	local Name = Frame:WaitForChild("Name")

	-- Settings
	Total.Text = "Total: "..player.leaderstats.Money.Value
	Amount.Text = amount
	Name.Text = player.Name
	

	-- Tween
	local Tween1 = TweenService:Create(Frame, TweenInfo1, goals1)
	local Tween2 = TweenService:Create(Frame, TweenInfo2, goals2)

	Tween1:Play()
	wait(5)
	Tween2:Play()
end

-- Loops

while true do
	wait(10)
	addMoney()
	showPaycheck()
end

Script that loads and saves data:

-- References

local DataStoreService = game:GetService("DataStoreService")
local DataStore1 = DataStoreService:GetDataStore("DataStore1")



-- Events

game.Players.PlayerAdded:Connect(function(plr)
	userId = plr.UserId
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr
	
	local Money = Instance.new("IntValue")
	Money.Name = "Money"
	Money.Parent = leaderstats
	
	 
	
	
	local succes, errormessage = pcall(function()
		data = DataStore1:GetAsync(userId)
	end)	
	if succes then
		Money.Value = data
		print("Data loaded")
	end
	if errormessage then
		print(errormessage)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	userId = plr.UserId
	
	data = plr.leaderstats.Money.Value

	local success, errormessage = pcall(function()
		DataStore1:SetAsync(userId, data)
	end)
	if not success then
		print("Error saving data:", errormessage)
	else
		print("Data saved")
	end
end)


Thanks in advance

2 Likes

Ah, you’ve leveled up in your coding learning experience.

Enjoy the learning of Remote Events, Funcs, and Binds.
RemoteEvent | Documentation - Roblox Creator Hub

1 Like

So I should create a local script in the StarterPlayerScripts, paste the paycheck code into it and make sure to use a remote event to change the value on the server side?

1 Like

You should really read the documentation

Pretty much, there’s a picture I was looking for.

Remote Events and Callbacks | Documentation - Roblox Creator Hub

this docs better^

1 Like

I have used a remove event to change the value, and it changes, but it still won’t save the value. I have checked on the server side, and it does change, it just doesn’t save.

Could need to fire a third module that casts the client to update

What do you mean exactly? I don’t really understand

i’d look into UserId next.

-- Functions
function addMoney(userId)
	local player = game.Players:GetPlayerByUserId(userId)
	local amount = math.random(10,100)
	player.leaderstats.Money.Value = player.leaderstats.Money.Value + amount
end

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

local userId = plr.UserId
	
	-- Loops
	while true do
		wait(10)
		addMoney(userId)
		showPaycheck(userId)
	end
end)

I rewrote the script to save data, and somehow it worked. I think it had something to do with the scope of the data variable

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.