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

[Solved] Why is my 'end' order all screwed up?

So I'm making a script (not to go into details) that uses multiple if and then statements with lots of elseif. It all seems to work until I try to end them. The end) paranthases has a red line under it but when I remove the ) it says it needs that ). So when I change the order the only thing that works makes a blue line under a bunch of things saying there unknown globally. ive seen this problem before and I'm not sure how to fix it any help would be greatly appreciated! Sorry it looks so complicated/messy. Code

local sell = script.Parent
local egg = game.Workspace.Egg
local board = game.Workspace

sell.Touched:Connect(function(part)
	if part.Name == "LeftFoot" or part.Name == "RightFoot" then
		if part.Parent:FindFirstChild("Egg") then 
			wait(2)
			local egg = part.Parent:FindFirstChild("Egg")
			if egg and egg.Handle.Type.Value == Common then
				player.leaderstats.Dollars.Value = player.leaderstats.Dollars.Value + 5 --the 'players' gets blue underlined
				wait(2)
			elseif egg and egg.Handle.Type.Value == Uncommon  then --the rares/uncommons etc get a blue underline
				player.leaderstats.Dollars.Value = player.leaderstats.Dollars.Value + 10
				wait(2)
			else	
				wait(2)	
			end
		else 
			wait(3)	
		end
	end --switching the order of these ends messes up the things above
end) --this ) has a red line
end) --when I switch it this gets a red underline rest is good
	end
		end 

4 comments

0

Ow.

0

^ and again I say Ow!

0

i just had a lot of messages on a billboard gui ill try to clean it up but any ideas on the issue?

1 answer

✓ Accepted answer

You put in a few useless lines of code, as well as putting ends in for just the else's when you only need an end after an if then or an elseif then statement.

local sell = script.Parent
local egg = game.Workspace.Egg
local board = game.Workspace

sell.Touched:connect(function(part)
	if part.Parent:findFirstChild("Egg") then
		wait(2)
		local egg = part.Parent:findFirstChild("Egg")
		if egg and egg.Handle.Type.Value == 'Common' then
			player.leaderstats.Dollars.Value = player.leaderstats.Dollars.Value + 5
			wait(2)
		elseif egg and egg.Handle.Type.Value == 'Uncommon'  then
			player.leaderstats.Dollars.Value = player.leaderstats.Dollars.Value + 10
					wait(2)
		
			end
			wait(3)
			
		end
	end
end)

9 comments

0

Though that wait(3) is useless too, should just have the wait at line 14 be wait(5) at that point. because you're just forcing the script to wait 3 seconds to fully end at this point.

0

why did you change his FindFirstChild to findFirstChild

0

I'm just used to using findFirstChild, when I re-wrote it I just automatically did it. findFirstChild still works, it's just deprecated.

0

the wait(3) and some other useless lines are needed i removed a lot of thing like board.Text = "IMPORTANT STUFF" etc etc ill try this out tho

0

the apastrophes around uncommon and common, are they needed. They are a string values value

0

Yes, thats the only way for it to read what it says, unless you say local Common = 'Common' and local Uncommon = 'Uncommon'

0

Ok so i think i fixed it the player was underlined because i didnt define it which i chaged to, local player = game.Players:GetPlayerFromCharacter(part.Parent)

0

@IcyEvil don't write code that just works. Write code that is both good code and works. Deprecated code is not good code, what if something happens to findFirstChild? Deprecated code is bound to break at any point, should roblox ever remove them

0

@incapaz My bad, I'm just so used to writing it 'findFirstChild' but I'll remember to use the undeprecated version from now on.

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