I have a serverscript which initiates whenever a boss is defeated, however it is saying attempt to index nil with leaderstats when leaderstats very clearly exists.
The error is in the 1st line this is only a part of the code and that’s where the error occurs.
local store = game.Players.LocalPlayer:WaitForChild("leaderstats")
store.EXP.Value = store.EXP.Value + script.Parent.Config.EXP.Value--/RewardDivider
store.G.Value = store.G.Value + script.Parent.Config.Gold.Value--/RewardDivider
---------------
if store.LV.Value <=10 then
if store.EXP.Value >=store.LV.Value * store.LV.Value * 100 + 400 then
store.LV.Value = store.LV.Value + 1
end
end
if store.LV.Value >10 and store.LV.Value <=20 then
if store.EXP.Value >=store.LV.Value * store.LV.Value * 150 + 400 then
store.LV.Value = store.LV.Value + 1
end
end
if store.LV.Value >20 and store.LV.Value <30 then
if store.EXP.Value >=store.LV.Value * store.LV.Value * 200 + 400 then
store.LV.Value = store.LV.Value + 1
end
end
if store.LV.Value >=30 then
if store.EXP.Value >=store.LV.Value * store.LV.Value * store.LV.Value *10+ 400 then
store.LV.Value = store.LV.Value + 1
end
end
----------------
player.leaderstats.EXP.Value = store.EXP.Value
player.leaderstats.G.Value = store.G.Value
player.leaderstats.LV.Value = store.LV.Value
Doesn’t make a difference if there’s a waitforchild or not help would be greatly appreciated thank you
Well if your goal is to update every player that should be updated do something like
local players = game:GetService("Players") -- Player service
players.PlayerAdded:Connect(function(player)
local store = player:FindFirstChild("leaderstats") -- Will return nil or true, you do not need :WaitForChild in server scripts
if store then -- If leaderstats is found
store.EXP.Value = store.EXP.Value + script.Parent.Config.EXP.Value--/RewardDivider
store.G.Value = store.G.Value + script.Parent.Config.Gold.Value--/RewardDivider
---------------
if store.LV.Value <=10 then
if store.EXP.Value >=store.LV.Value * store.LV.Value * 100 + 400 then
store.LV.Value = store.LV.Value + 1
end
end
if store.LV.Value >10 and store.LV.Value <=20 then
if store.EXP.Value >=store.LV.Value * store.LV.Value * 150 + 400 then
store.LV.Value = store.LV.Value + 1
end
end
if store.LV.Value >20 and store.LV.Value <30 then
if store.EXP.Value >=store.LV.Value * store.LV.Value * 200 + 400 then
store.LV.Value = store.LV.Value + 1
end
end
if store.LV.Value >=30 then
if store.EXP.Value >=store.LV.Value * store.LV.Value * store.LV.Value *10+ 400 then
store.LV.Value = store.LV.Value + 1
end
end
----------------
player.leaderstats.EXP.Value = store.EXP.Value
player.leaderstats.G.Value = store.G.Value
player.leaderstats.LV.Value = store.LV.Value
else -- If leaderstats isn't found
warn("Leaderstats not found for " .. player.Name .. "!")
end
end)
I believe that would work, also make sure you create leaderstats
Also you have a lot of ifs. These aren’t good in certain situations and you should use elseif depending on what it does. Because if you check one value and it’s true then change it and check it again, it could return true multiple times causing stuff you don’t want
For example
I want this to check if someone has 10 money or less
If they have less than 10 or = to 10 then I want them to gain money
If they have more I want them to lose money
if money.Value <= 10 then
money.Value += 5
end
if money.Value > 10 then
money.Value -= 10
end
That will add 5 then because it’s now over 10 it will then remove 10
To make it only do one thing I would do
if money.Value <= 10 then -- If met it doesn't run the elseif
money.Value += 5
elseif money.Value > 10 then
money.Value -= 10
end
That will give me 5 cash if I have 10 or less and will not remove 10 after
If I have over 10 it will remove 10 and that’s all
Now this doesn’t mean do it for all ifs, I don’t exactly know what you’re doing but if you do have issues with what’s given/taken/changed this could be the issue or you may not have any issues, of course test it first. Hope this helps