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

Why doesn't this work?

Asked by 9 years ago

This is a script for a terminal, and I'm wondering why this isn't working. Please help!

function onTouched(part)
    local character = part.Parent
    local player = game.Players:GetPlayerFromCharacter( character )
    if character and player then
        local team = player.TeamColor
        if team == BrickColor.new("Medium stone grey") then --Raider Group Color
    for progress = 5, 100, 5 do
            wait(60)
            local hint = Instance.new('Hint', workspace)
            hint.Text = progress .. "%"     
    game.Workspace.Colors.A.BrickColor = BrickColor.new("Medium stone grey") --Raider Team Color (Change All)
    game.Workspace.Colors.B.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Colors.C.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Colors.D.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Colors.E.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Colors.F.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Colors.G.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Colors.H.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Colors.I.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Colors.J.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Colors.K.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Colors.L.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Colors.M.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Colors.N.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Colors.O.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Colors.P.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Colors.Q.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Colors.R.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Colors.S.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Colors.T.BrickColor = BrickColor.new("Medium stone grey")    
    game.Workspace.Colors.U.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Colors.V.BrickColor = BrickColor.new("Medium stone grey")
    game.Workspace.Light.Part.BrickColor = BrickColor.New("Medium stone grey")
    end
        elseif team == BrickColor.new("Really red") then
    game.Workspace.Colors.A.BrickColor = BrickColor.new("Really red") --Home Team Group Color (Change All)
    game.Workspace.Colors.B.BrickColor = BrickColor.new("Really red")
    game.Workspace.Colors.C.BrickColor = BrickColor.new("Really red")
    game.Workspace.Colors.D.BrickColor = BrickColor.new("Really red")
    game.Workspace.Colors.E.BrickColor = BrickColor.new("Really red")
    game.Workspace.Colors.F.BrickColor = BrickColor.new("Really red")
    game.Workspace.Colors.G.BrickColor = BrickColor.new("Really red")
    game.Workspace.Colors.H.BrickColor = BrickColor.new("Really red")
    game.Workspace.Colors.I.BrickColor = BrickColor.new("Really red")
    game.Workspace.Colors.J.BrickColor = BrickColor.new("Really red")
    game.Workspace.Colors.K.BrickColor = BrickColor.new("Really red")
    game.Workspace.Colors.L.BrickColor = BrickColor.new("Really red")
    game.Workspace.Colors.M.BrickColor = BrickColor.new("Really red")
    game.Workspace.Colors.N.BrickColor = BrickColor.new("Really red")
    game.Workspace.Colors.O.BrickColor = BrickColor.new("Really red")
    game.Workspace.Colors.P.BrickColor = BrickColor.new("Really red")
    game.Workspace.Colors.Q.BrickColor = BrickColor.new("Really red")
    game.Workspace.Colors.R.BrickColor = BrickColor.new("Really red")
    game.Workspace.Colors.S.BrickColor = BrickColor.new("Really red")
    game.Workspace.Colors.T.BrickColor = BrickColor.new("Really red")   
    game.Workspace.Colors.U.BrickColor = BrickColor.new("Really red")
    game.Workspace.Colors.V.BrickColor = BrickColor.new("Really red")
    game.Workspace.Light.Part.BrickColor = BrickColor.New("Really red")
    game.Workspace.Message:Remove()
        end
    end
end

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

First: tab your code correctly.

Second: Don't have 20 lines that do the same thing.

Use a loop over the children of the model

for _, part in pairs(workspace.Colors:GetChildren()) do
    part.BrickColor = BrickColor.new("Medium stone grey")
end
workspace.Light.Part.BrickColor = BrickColor.new("Medium stone grey")

Now you don't have to "change all"" (you should never have to do that in a good program).


You have the above snippet repeated twice; the only difference is that you are using the color of team. So why not just do the above regardless of the team to just match team?

function onTouched(part)
    local character = part.Parent
    local player = game.Players:GetPlayerFromCharacter( character )
    if character and player then
        local team = player.TeamColor
        if team == BrickColor.new("Medium stone grey") then --Raider Group Color
            for progress = 5, 100, 5 do
                wait(60)
                local hint = Instance.new('Hint', workspace)
                hint.Text = progress .. "%"
            end
        elseif team == BrickColor.new("Really red") then
            game.Workspace.Message:Remove()
        end
        -- These four lines replace forty in the original...
        for _, part in pairs(workspace.Colors:GetChildren()) do
            part.BrickColor = team
        end
        workspace.Light.Part.BrickColor = team
        --
    end
end

Remaining potential issues:

  • You tried to :Remove() workspace.Message, but it's possible that there isn't a such thing as workspace.Message. That would cause an error. You should check if workspace:FindFirstChild("Message") then first.

  • You didn't include anything that will ever run onTouched. Is there a connection line that you just didn't quote here? If not, there should be one.

Ad

Answer this question