What is wrong with the if statement?

This is a server script/ normal script inside server script service.
What is wrong is when 1 player joins the person is allocated a base and the value of owner changes to their name whereas when player 2 joins the value of owner doesn’t change

game.Players.PlayerAdded:Connect(function(plr)


    if workspace.Bases.Blue.Owner.Value == "" then
        workspace.Bases.Blue.Owner.Value = plr.Name

    elseif workspace.Bases.Red.Owner.Value == "" then
        workspace.Bases.Red.Owner.Value = plr.Name

    elseif workspace.Bases.Yellow.Owner.Value == "" then
        workspace.Bases.Yellow.Owner.Value = plr.Name

    elseif workspace.Bases.Green.Owner.Value == "" then
        workspace.Bases.Green.Owner.Value = plr.Name



end
  1. You forgot an end). (30 char)

Edit: Because it checks if the Value is nil. When 1st player joins, it sets the value to their name, and thus making it not nil. When player 2 joins, the value is no longer nil because it has the name of the first player.

3 Likes

This should be the fix (It was just missing the end for the if statement and the parenthesis on the last end):

game.Players.PlayerAdded:Connect(function(plr)


    if workspace.Bases.Blue.Owner.Value == "" then
        workspace.Bases.Blue.Owner.Value = plr.Name

    elseif workspace.Bases.Red.Owner.Value == "" then
        workspace.Bases.Red.Owner.Value = plr.Name

    elseif workspace.Bases.Yellow.Owner.Value == "" then
        workspace.Bases.Yellow.Owner.Value = plr.Name

    elseif workspace.Bases.Green.Owner.Value == "" then
        workspace.Bases.Green.Owner.Value = plr.Name
    end


end)
1 Like
local Teams = {workspace.Bases.Blue.Owner, workspace.Bases.Red.Owner, workspace.Bases.Yellow.Owner, workspace.Bases.Green.Owner}

game.Players.PlayerAdded:Connect(function(plr)
     for i,v in pairs(Teams) do
          if v.Value == "" then
               v.Value = plr.Name
               break
          end
     end
end

Adding same codes aren’t looks nice. so there’s improvement.

  • Use tables you want to interact
  • Using the same if statement, so it can be replaced using for i,v in pairs() do

If there’s a error, please reply me.

2 Likes