Leaderstats Script Not Working. Please Help!

Hello Fellow Developers!

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

end)

game.Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, {
[“Coins”] = Player.leaderstats.Coins.Value;
[“Orbs”] = Player.leaderstats.Orbs.Value;
})
end)

I don’t know if its something wrong with roblox or not, but i really hope someone can help me and solve this mystery! Thank You So Much!

Edit: If you’d like to test it out for yourself, here’s the game: Coin farming Simulator - Roblox

2 Likes

You need to parent all of your stuff that is created by Instance.new()

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”
leaderstats.Parent = Player
local Coins= Instance.new(“IntValue”, leaderstats)
Coins.Name = “Coins”
Coins.Value = 0
Coins.Parent = leaderstats
local Orbs= Instance.new(“IntValue”, leaderstats)
Orbs.Name = “Orbs”
Orbs.Value = 0
Orbs.Parent = leaderstats

local Data = DataStore:GetAsync(Player.UserId)
if Data then
	Coins.Value = Data.Coins 
	Orbs.Value = Data.Orbs 
end

end)

game.Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, {
[“Coins”] = Player.leaderstats.Coins.Value;
[“Orbs”] = Player.leaderstats.Orbs.Value;
})
end)

Omg! im so dumb, let me try it xD

They are already parented, at the second argument in Instance.new(“type”, parent)

Oh that’s true. Thanks for pointing that out, I didn’t see that. But I think parenting it while its in a variable will change something.

No worries :upside_down_face:

maybe its cause its late, but im confused. Instance.new(IntValue, Parent) ?

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.

im so sorry i dont know what you mean, im only a kid and im not the best at scripting.

i added leaderstats.Parent = Player in.

o wait yea it didnt work

1 Like

That’s fine. Here’s the script applying what I mean.

local DataStoreService = game:GetService(“DataStoreService”)
local DataStore = DataStoreService:GetDataStore(“MoneyStats”)

game.Players.PlayerAdded:Connect(function(Player)
local leaderstats = Instance.new(“Folder")
leaderstats.Name = “leaderstats”
leaderstats.Parent = Player
local Coins= Instance.new(“IntValue”)
Coins.Name = “Coins”
Coins.Value = 0
Coins.Parent = leaderstats
local Orbs= Instance.new(“IntValue”)
Orbs.Name = “Orbs”
Orbs.Value = 0
Orbs.Parent = leaderstats

local Data = DataStore:GetAsync(Player.UserId)
if Data then
	Coins.Value = Data.Coins 
	Orbs.Value = Data.Orbs 
end

end)

game.Players.PlayerRemoving:Connect(function(Player)
DataStore:SetAsync(Player.UserId, {
[“Coins”] = Player.leaderstats.Coins.Value;
[“Orbs”] = Player.leaderstats.Orbs.Value;
})
end)
[/quote]

1 Like

ok, ill try it… :slight_smile:

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

You can learn more about pcall here.


But the fact that you’re getting 100 money and 100 orbs is strange…

2 Likes

ok, i did that, and im still having the same issue…

Have you set the settings?


PS: Allows roblox studio to access datastores, clients already have access.

yes i have, but what does that have to do with currencies becoming the same? it also happens in the main game out of studio

Could we see the script to collecting the orbs?

There could be a separate script that’s also connected to PlayerRemoving, which sets the value to something, something…

ok, both coins and orbs are the same scripts, just changed in currencies

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

    script.Parent.Transparency = 1
    script.Disabled = true
    wait(waittime)
    script.Parent.Transparency = 0
    script.Disabled = false


end

end

script.Parent.Touched:connect(onTouched)

1 Like