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

Why doesn't the elseif part of this script not work?

Why doesn't the elseif part of this script not work? Everything else in the whole script works besides this one part, please help.

bin = script.Parent

function onTouched(bin)
local character = bin.Parent
local player = game.Players:GetPlayerFromCharacter(character)
local team = player.TeamColor
    if character and player then
        if team == BrickColor.new("Medium stone grey") then --Raider Group Color
		if workspace:FindFirstChild("Active") then	
			
		end
		if workspace:FindFirstChild("Camera") then
			game.Workspace.Camera.Name = "Active"
			game.Workspace.Light.BrickColor = team
			local hint = Instance.new('Hint', workspace)
			hint.Text = "Raiders have taken the base! 20 Minutes Till Raiders have won!"
			wait(3)
			game.Workspace.Message:Remove()
			game.Workspace["Owned By: (Group Name)"].Name = "Owned By: Raiders"
			game.Workspace.Glass.BrickColor = BrickColor.New("Medium stone grey") --Raider Group Color
       	 for _, part in pairs(game.Workspace.Colors:GetChildren()) do
            part.BrickColor = team
	end
		if workspace:FindFirstChild("Camera") then
            for progress = 5, 100, 5 do
                wait(60)
				local hint = Instance.new('Hint', workspace)
                hint.Text = progress .. "%"
				end
			end
        elseif team == BrickColor.new("Really red") then --Main Group Team Color
			if workspace:FindFirstChild("Active") then
						game.Workspace.Active.Name = "Camera"
	        			game.Workspace.Light.BrickColor = team
						game.Workspace["Owned By: Raiders"].Name = "Owned By: (Group Name)" --Insert Group Name Here
						local hint = Instance.new('Hint', workspace)
						hint.Text = "(Group Name) has taken back the base!"  --Insert Group Name Here
						wait(3)
						hint:Remove()		
						for _, part in pairs(game.Workspace.Colors:GetChildren()) do
            			part.BrickColor = team
					end
				end
			end
		end
	end
end

bin.Touched:connect(onTouched)

2 comments

0

Is it erroring?

0

No

2 answers

I believe your problem has to do with the placement of you elseif statement. It will help a lot if you properly space every thing out (see the Lua Style Guide for more information):

bin = script.Parent

function onTouched(bin)
	local character = bin.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	local team = player.TeamColor

	if character and player then
		if team == BrickColor.new("Medium stone grey") then --Raider Group Color
			if workspace:FindFirstChild("Active") then  

			end
			if workspace:FindFirstChild("Camera") then
				game.Workspace.Camera.Name = "Active"
				game.Workspace.Light.BrickColor = team
				local hint = Instance.new('Hint', workspace)
				hint.Text = "Raiders have taken the base! 20 Minutes Till Raiders have won!"
				wait(3)
				game.Workspace.Message:Remove()
				game.Workspace["Owned By: (Group Name)"].Name = "Owned By: Raiders"
				game.Workspace.Glass.BrickColor = BrickColor.New("Medium stone grey") --Raider Group Color
				for _, part in pairs(game.Workspace.Colors:GetChildren()) do
					part.BrickColor = team
				end
				if workspace:FindFirstChild("Camera") then
					for progress = 5, 100, 5 do
						wait(60)
						local hint = Instance.new('Hint', workspace)
						hint.Text = progress .. "%"
					end
				end
			elseif team == BrickColor.new("Really red") then --Main Group Team Color 
				if workspace:FindFirstChild("Active") then
					game.Workspace.Active.Name = "Camera"
					game.Workspace.Light.BrickColor = team
					game.Workspace["Owned By: Raiders"].Name = "Owned By: (Group Name)" --Insert 		Group Name Here
					local hint = Instance.new('Hint', workspace)
					hint.Text = "(Group Name) has taken back the base!"  --Insert Group Name Here
					wait(3)
					hint:Remove()       
					for _, part in pairs(game.Workspace.Colors:GetChildren()) do
						part.BrickColor = team
					end
				end
			end
		end
	end
end

bin.Touched:connect(onTouched)

As you can see, the elseif statement is going to the if workspace:FindFirstChild("Camera") thenline instead of the line I think you want it to go to, the if team == BrickColor.new("Medium stone grey") then --Raider Group Color line. In order to fix it, if that is your problem, then you simply need rearrange you end statements:

bin = script.Parent

function onTouched(bin)
	local character = bin.Parent
	local player = game.Players:GetPlayerFromCharacter(character)
	local team = player.TeamColor

	if character and player then
		if team == BrickColor.new("Medium stone grey") then --Raider Group Color
			if workspace:FindFirstChild("Active") then  

			end
			if workspace:FindFirstChild("Camera") then
				game.Workspace.Camera.Name = "Active"
				game.Workspace.Light.BrickColor = team
				local hint = Instance.new('Hint', workspace)
				hint.Text = "Raiders have taken the base! 20 Minutes Till Raiders have won!"
				wait(3)
				game.Workspace.Message:Remove()
				game.Workspace["Owned By: (Group Name)"].Name = "Owned By: Raiders"
				game.Workspace.Glass.BrickColor = BrickColor.New("Medium stone grey") --Raider Group Color
				for _, part in pairs(game.Workspace.Colors:GetChildren()) do
					part.BrickColor = team
				end
				if workspace:FindFirstChild("Camera") then
					for progress = 5, 100, 5 do
						wait(60)
						local hint = Instance.new('Hint', workspace)
						hint.Text = progress .. "%"
					end
				end
			end
		elseif team == BrickColor.new("Really red") then --Main Group Team Color 
			if workspace:FindFirstChild("Active") then
				game.Workspace.Active.Name = "Camera"
				game.Workspace.Light.BrickColor = team
				game.Workspace["Owned By: Raiders"].Name = "Owned By: (Group Name)" --Insert 		Group Name Here
				local hint = Instance.new('Hint', workspace)
				hint.Text = "(Group Name) has taken back the base!"  --Insert Group Name Here
				wait(3)
				hint:Remove()       
				for _, part in pairs(game.Workspace.Colors:GetChildren()) do
					part.BrickColor = team
				end
			end
		end
	end
end


bin.Touched:connect(onTouched)

2 comments

0

Woah... I somehow got more rep than you... Either you weren't on very much or I'm just amazing :D (jk)

0

Haha, I have a tough semester (Calculus, Chem, Physics, English) so I don't have much spare time :P

It's because of there are two of the same variable which you have as one being the script's parent bin = script.Parent and the other being the object that hit the script's parent function onTouched(bin). It's confusing the script and the script will basically ignore the variable you have in the event. Instead of searching for the part's parent that hit the brick to begin with, you're trying to find the script's parent, which leads to the script not being able to find the player. All you have to do is change these two lines,

function onTouched(bin)
    local character = bin.Parent

to;

function onTouched(hit)
    local character = hit.Parent

BEFORE ANYONE DOWNVOTES THIS, YOU TRY IT. I HAVE RENAMED THE TAG EARLIER WITH ONE OF THE POSTER'S ORIGINAL SCRIPTS AND IT DETECTED WHO HIT IT.

I do not believe I can emphasize that enough.

2 comments

0

This is an error you'll want to correct, and there may be more. I never looked over the code thoroughly, so I never changed much in my answer. However, once you start formatting your code so it's more readable, errors will start popping out at you.

0

For fricks sakes guys, this is the actual fix and I have tested it personally with the poster's previous script.

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