Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

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

Asked by 9 years ago

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)
0
Is it erroring? YellowoTide 1992 — 9y
0
No RexSays 0 — 9y

2 answers

Log in to vote
0
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
9 years ago

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)
0
Woah... I somehow got more rep than you... Either you weren't on very much or I'm just amazing :D (jk) EzraNehemiah_TF2 3552 — 9y
0
Haha, I have a tough semester (Calculus, Chem, Physics, English) so I don't have much spare time :P BlackJPI 2658 — 9y
Ad
Log in to vote
-2
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
9 years ago

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.

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. BlackJPI 2658 — 9y
0
For fricks sakes guys, this is the actual fix and I have tested it personally with the poster's previous script. M39a9am3R 3210 — 9y

Answer this question