Still have questions? Join our Discord server and get real time help.
0

# Auto-rank script with XP, so it will change their cash when they reach a certain amount of XP, Help?

Hello, I'm making an auto-stat change script which changes someone rank when they reach a certain amount of XP, I've added a few 'end' lines because when I start a line with "if" it will make me put a 'end' at the end, I don't know if this is the problem, here is the script, the script is in a a part which is stretched across the world, so they are always touching it.

local ting = 0

function onTouched(hit)

if ting == 0 then
ting = 1
check = hit.Parent:FindFirstChild("Humanoid")

if check ~= nil then

local user = game.Players:GetPlayerFromCharacter(hit.Parent)

if stats ~= nil then
local cash = stats:findFirstChild("Rank")
local xp = stats:findFirstChild("XP")
if xp.Value == 250 then
cash.Value  = "Private"
elseif xp.Value == 7500 then
cash.Value = "Private First Class"
elseif xp.Value == 20000 then
cash.Value = "Corporal"
elseif xp.Value == 21550 then
cash.Value = "Warrant Officer 2"
elseif xp.Value == 23500 then
cash.Value = "Warrant Officer 1"
elseif xp.Value == 25000 then
cash.Value = "Sergeant"
elseif xp.Value == 30000 then
cash.Value = "Master Sergeant"
elseif xp.Value == 35000 then
cash.Value = "Capitan"
elseif xp.Value == 36500 then
cash.Value = "Second Lieutenant"
elseif xp.Value == 37500 then
cash.Value = "First Lieutenant"
elseif xp.Value == 40000 then
cash.Value = "Cononel"
elseif xp.Value == 45000 then
cash.Value = "Commander"
elseif xp.Value == 50000 then
elseif xp.Value = 65000 then
cash.Value = "Commanding Officer"
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end
end)

ting = 0
end
end
end

script.Parent.Touched:connect(onTouched)


• THANKS
0
learn indenting plox User#19524 106 — 10mo
0
?? Citrisz 9 — 10mo
0
one thing, you don't need an end for every elseif, just one for the first if statement Blue_Deku19 287 — 10mo
0
Ok Citrisz 9 — 10mo

0
berezaa 15
10 months ago

You don't want to check if a Player's XP equals a certain amount in order to assign them a rank, because what if they go over that amount?

Instead, check if the player has equal or more XP than what is required for that rank, in order of the highest rank to the lowest rank. For example:

if xp >= 10000 then
elseif xp >= 500 then
rank = "Colonel"
else
rank = "Peasant"
end


Additionally, you have way too many END statements. You only need END statements for functions, IF and WHILE statements. ELSEIF and ELSE statements "continue" IF statements and don't require their own END.

0
Thanks so much berezaa! Just a couple of things, 1: I don't understand like if you do, elseif and then else, do you continue that process for all the ranks, or is the else just at the end. 2: I can't believe you helped me, Your so famous and you went to help me, I'm only a small developer with less than 1 thousand place visits, Thank you so much for helping :D Citrisz 9 — 10mo
0
Your amazing btw :) Citrisz 9 — 10mo
0
What happens is, if you have an IF statement, and the condition isn't met, Lua moves on to the next statement. If the next statement is an ELSEIF, then it checks that and keeps going if it is false. ELSEIF functions the same way as an IF, except it doesn't require its own END and it only gets checked if the previous statements fail. ELSE is like saying "if nothing above this worked, do me!". berezaa 15 — 10mo
0
Ok, thanks again! Citrisz 9 — 10mo
0
its u berezaa!!! greatneil80 1211 — 9d