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