Script not finding leaderstats value error

Hey there I am having a script error where it can’t find my leaderstats value I think it is because I have 2 values for the leaderstats. I am trying to make a Diamond collect script that when you touch it you get 1 Diamond. Here’s the leaderstats:

local Players = game:GetService(“Players”)

local function leaderboardSetup(player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = player

local coin = Instance.new("IntValue")
coin.Name = "Coins"
coin.Value = 0
coin.Parent = leaderstats

local Diamond = Instance.new("IntValue")
Diamond.Name = "Daimonds"
Diamond.Value = 0
Diamond.Parent = leaderstats

end
Players.PlayerAdded:Connect(leaderboardSetup)

Here’s the Diamond collect script:

db = false

script.Parent.Touched:Connect(function(hit)
if db == false then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
player.leaderstats.Diamonds.Value = player.leaderstats.Diamonds.Value + 1
script.Parent:Destroy()
end
end)

I don’t know why the script isn’t finding the Diamonds in leaderstats when I clearly have a value for it in my leaderstats script!

If anyone can help it would be very apprieciated!

You misspelled “Diamonds” in the leaderstats.

3 Likes

Sorry guys I was in a rush at that time:/

also guys I have one more problem with leaderstats I have a leaderstats save but it won’t save both values here’s script

local dataStoreService = game:GetService(“DataStoreService”)
local leaderstatsDataStore = dataStoreService:GetGlobalDataStore(“leaderstats”)

local loaded = {}

game.Players.PlayerAdded:connect(function(player)
local leaderstats = player:WaitForChild(“leaderstats”)
if player.UserId > 0 and player.Parent then
local leaderstatsData = leaderstatsDataStore:GetAsync(player.UserId)
if leaderstatsData ~= “Request rejected” then
if leaderstatsData then
for i, stat in ipairs(leaderstats:GetChildren()) do
local value = leaderstatsData[stat.Name]
if value then
stat.Value = value
end
end
end
loaded[player] = true
end
end
end)

game.Players.PlayerRemoving:connect(function(player)
local leaderstats = player:FindFirstChild(“leaderstats”)
if leaderstats then
if loaded[player] then
local leaderstatsData = {}
for i, stat in ipairs(leaderstats:GetChildren()) do
leaderstatsData[stat.Name] = stat.Value
end
leaderstatsDataStore:SetAsync(player.UserId, leaderstatsData)
end
end
loaded[player] = nil
end)

I think he meant you added an extra”s” in your player.leaderstats.Diamonds path

Try the video I made on leaderstats saving:

No, he spelled it like this “Daimonds” under local Diamond = Instance.new(“IntValue”)

1 Like

I can’t see much wrong with the script but I think it would be easier for everyone if you formatted so we could work through it a bit easier however, make sure that none of your values are equal to nil. This is because ipairs() will yield (stop) if it comes across any value equal to nil