So i have a leaderstats script with 2 currencies, Coins and Orbs. Everything in the script is fine. there’s no errors. but when i play the game this happens: ill go get some coins (say i get like 100) i get ZERO Orbs. Then i leave the game, and rejoin and i will have 100 coins and 100 orbs. I’ve looked at many things on this devforum, tried many different leaderstats, and still, the same issue, I’ve looked to see if anyone else has this issue and found nobody. Here’s the script!
local DataStoreService = game:GetService(“DataStoreService”)
local DataStore = DataStoreService:GetDataStore(“MoneyStats”)
game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new(“Folder”, Player)
leaderstats.Name = “leaderstats”
local Coins= Instance.new(“IntValue”, leaderstats)
Coins.Name = “Coins”
Coins.Value = 0
local Orbs= Instance.new(“IntValue”, leaderstats)
Orbs.Name = “Orbs”
Orbs.Value = 0
local Data = DataStore:GetAsync(Player.UserId)
if Data then
Coins.Value = Data.Coins
Orbs.Value = Data.Orbs
end
Try removing the parent in the variable. I’m pretty sure it might change something. It’s like: local part = game.Workspace.Part:Clone().Parent = game.Workspace
You wouldn’t want to specify the parent in the variable.
One thing I would like to point out, is that you have no safety checks, like what if the DataStore fails to load, or even save? With pcall, you can determine if there are any errors, and catch them, if there is any. Inserting a pcall into a loop, with certain tries (to prevent leaking) will add some safety to saving and loading;
local success, errorMessage = false, ""
for i = 1, 10, 1 do -- It gives up once it has tried 10 times. It has to give up to prevent memory leaks.
success, errorMessage = pcall(function()
-- Do the data saving / loading
end
if success then break end -- If it successfully saved / loaded then quit retrying.
wait(10) -- Wait a while before retrying again.
end
waittime = 5 – Time Between each hit
amnt = 10 --how much you get for it
function onTouched(part)
local h = part.Parent:findFirstChild(“Humanoid”)
if (h~=nil) then
local thisplr = game.Players:findFirstChild(h.Parent.Name)
if (thisplr~=nil) then
local leaderstatsstats = thisplr:findFirstChild(“leaderstats”)
if (leaderstatsstats~=nil) then
local orbs = leaderstatsstats:findFirstChild(“Orbs”)
if (orbs~=nil) then
orbs.Value = orbs.Value + amnt
end
end
end