Hello everyone, I'm making a script that will change the players leader board rank when they reach a certain amount of XP, I was recently helped out by berezaa, but I've got stuck again
Here is my script I've been using:
(its not a local script btw)
function onPlayerEntered(newPlayer) local user = game.Players:findFirstChild(newPlayer) local stats = user:findFirstChild("leaderstats") while true do if stats ~= nil then local rank = stats:findFirstChild("Rank") local xp = stats:findFirstChild("XP") if xp >= 75000 then rank = "Commanding Officer" elseif xp >= 65000 then rank = "General" elseif xp >= 50000 then rank = "Admarial" elseif xp >= 45000 then rank = "Cononel" elseif xp >= 35000 then rank = "Captian" elseif xp >= 37500 then rank = "First LT" elseif xp >= 36500 then rank = "Second LT" elseif xp >= 30000 then rank = "Master Sergeant" elseif xp >= 25000 then rank = "Sergeant" elseif xp >= 23500 then rank = "Warrant Officer 1" elseif xp >= 21550 then rank = "Warrant Officer 2" elseif xp >= 20000 then rank = "Corparol" elseif xp >= 7500 then rank = "Private First Class" elseif xp >= 250 then rank = "Private" elseif xp >= 550000 then rank = "Moderator" elseif xp >= 750000 then rank = "Administrator" elseif xp >= 1500000 then rank = "Head Administrator" elseif xp >= 3500000 then rank = "Peace Keeper" elseif xp >= 5500000 then rank = "The Victorious" elseif xp >= 0 then print("M8 he just getting started!") end end end end game.Players.ChildAdded:connect(onPlayerEntered)
If you could help I'd really appreciate it!
There was also an error code:
Workspace.RA.Script:3: attempt to index local 'user' (a nil value)
The problems are on the lines where you check the xp
and rank
. You're checking if the object itself equals a number, which will be false
. You should add a .Value
for each time you want to check the value/change it.
local function onPlayerEntered(newPlayer) -- Globals are bad practice. Use locals! local stats = newPlayer:FindFirstChild("leaderstats") -- findFirstChild deprecated if stats then local rank = stats.Rank local xp = stats.XP if xp.Value >= 75000 then-- Check value! rank.Value = "Commanding Officer"--changing value elseif xp.Value >= 65000 then rank.Value = "General" print("M8 he just getting started!") end end end game.Players.PlayerAdded:Connect(onPlayerEntered) -- connect is deprecated! and use PlayerAdded
On your own you will add the .Value
.
Now your indentation. Your code is very messy. It is important to indent your code for the sake of readability. Though indentation does not affect the output of your code, it is important to have it indented. In other programming languages such as Python (not sure if there's other languages), it is indentation-sensitive. There is no end
keyword that signifies the end of a function, for, while or if like in Lua.
Finally, do not use ChildAdded
to listen for a player joining, as ChildAdded
fires for any object added, including non-Player objects, and it is possible to parent non-Player objects to Players
, which can cause problems with poorly written code.