So I have a script that loads if the player's money equals -1, but the data loads slower than the script. It looks like this:
LocalScript that needs to wait until the data is loaded:
local money = game.Players.LocalPlayer:WaitForChild("leaderstats").Money
if money.Value == -1 then
-- my script here
end
Script that saves and loads data:
local DataStoreService = game:GetService('DataStoreService')
local DataStore = DataStoreService:GetDataStore('CashSaveSystem')
game.Players.PlayerAdded:connect(function(player)
local leader = Instance.new('Folder', player)
leader.Name = "leaderstats"
local money = Instance.new("IntValue", leader)
money.Name = "Money"
money.Value = DataStore:GetAsync(player.UserId) or 250
DataStore:SetAsync(player.UserId, money.Value)
money.Changed:connect(function()
DataStore:SetAsync(player.UserId, money.Value)
end)
end)
game.Players.PlayerRemoving:connect(function(player)
DataStore:SetAsync(player.UserId, player.leaderstats.Money.Value)
end)
I know the data storing script could be better, but for now I want to leave it as it is. My only problem is: How to make the script wait until the data is loaded?

8 comments
I misunderstood your question
what do you mean
like, he spent all the money and now money's value is 0
if you want to check if their money is 0 do an elseif or else after the if statement
elseif money.Value == 0 then
Your code is so confusing lol
if you want to accept my answer the accept answer is under my answer
i only see the report button :(