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
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!
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 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