Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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.

The script is in a normal script, but please help if you can!



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)
		local stats = user:findFirstChild("leaderstats")

		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
				cash.Value = "Admarial"
			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

4 comments

0

learn indenting plox

0

??

0

one thing, you don't need an end for every elseif, just one for the first if statement

0

Ok

1 answer

✓ Accepted answer

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
	rank = "Admiral"
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.

5 comments

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

0

Your amazing btw :)

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

0

Ok, thanks again!

0

its u berezaa!!!

This discussion is preserved as it was. New answers, comments, and votes are closed.