How To Make An If Statement

Hi, I’m really new at coding lua. I made this if statement to make a player change teams to a team called Labyrinth Hunters if they touch a brick called LHblock and if they’re on a team called No Team.

Here’s My Code

local Player = game:GetService(“Players”).Player1

local Team = game:GetService(“Teams”)[“No Team”]

local LHcolor = “Really Blue”

if game.Workspace.LHblock.Touched:Connect and Player.Team == Team then

RemoteEvent:FireServer(BrickColor.new(LHcolor))

game.ReplicatedStorage.ChangeTeam.OnServerEvent:Connect(function(player, teamColor)

player.TeamColor = teamColor

end))

end

Please tell me what I’m doing wrong. Thanks for your time.

2 Likes

14 posts were merged out for being off-topic.

How would I save the data so when the player comes back they will be on Labyrinth Hunters team?

1 Like
if true then
--Code
end

That should still work, without an else in it.

3 Likes

Your trying to connect an event within a condition in an if statement.

2 Likes

There are a few things wrong with the code that just adding an else statement wouldn’t fix.
You are not passing any function into the Touched event of LHblock. The use of FireServer suggests that you are doing this in a local script which doesn’t make sense since you might be better off managing teams from the server side.

In a server script:

local players = game:GetService("Players")
local noTeam = game:GetService("Teams")["No Team"]
local team = game:GetService("Teams")["Labyrinth Hunters"]

-- Add a function to run when the brick is touched.
workspace.LHblock.Touched:Connect(function(hit)
	-- Check that the thing touching the brick is a player and that they have no team.
	local player = players:GetPlayerFromCharacter(hit.Parent)
	if (player and player.TeamColor == noTeam.TeamColor) then
		-- Change the player's team.
		player.Team = team
		player.TeamColor = team.TeamColor
	end
end)
8 Likes

Where would I put the script, I put my original script in workspace…

1 Like

Edit: Didn’t see the answer above while I was typing this.
Instead of this. Try this out:

CLIENT:

local Player = game:GetService(“Players”).LocalPlayer
local Team = game:GetService(“Teams”)[“No Team”]
local LHcolor = “Really Blue”

workspace.LHblock.Touched:Connect(function(hit)
   local humanoid = hit.Parent:FindFirstChild("Humanoid")
   if humanoid then
         if Player.Team == Team then
             RemoteEvent:FireServer(BrickColor.new(LHcolor))
         end
   end
end)

SERVER:

 game.ReplicatedStorage.ChangeTeam.OnServerEvent:Connect(function(player, teamColor)
     player.TeamColor = teamColor
 end)
3 Likes

@Blokav gives a very good point about not handling this .Touched event from the client and just doing it on the server. The FireServer method for a RemoteEvent is kind of pointless in this scenario.

1 Like

else and elseif are not at all needed in an if statement; they are merely statements that exist to extend the if statement to do something else on fail.

local testMe = true
if testMe == true then
    print("This if statement works! Without an else!")
end

You can also entirely dump the == true as if there are no comparison operators it simply checks if the value is true.

This here will work:

local testMe = true
if testMe then
    print("This if statement works! Without an else!")
end

Edit: You can also open run these in the console at the bottom of the screen if you need to verify whether I’m telling the truth or not.

3 Likes

As you’re new, it’d help more reading through some of Roblox tutorials on the developer hub, they have basic tutorials on there for getting started with Roblox and Lua

2 Likes

What do you mean by CLIENT:

And

SERVICE:

Is that where the script is supposed to go?

Client is scripts handled locally on the players device, while server scripts apply to everyone in the game on that server. Server scripts are often in server script storage or in the workplace. Client scripts are in starter player scripts or in starter player gui.

1 Like

This worked, thanks! However do you know how to make it so when the player rejoins they will stay on that team?

You can store a players team using Data Store. Going in to it with no knowledge, i recommend Alvin Blox Tutorial which he explains well and I learned everying thing i know so far from him.
Here is a link: Channel:https://www.youtube.com/channel/UCp1R0TBvgM7gj0rwTYULmSA
Data Store:https://www.youtube.com/watch?v=DkYupSBUpes&t=674s

Someone just asked this, Making Teams Permanent - #10 by Play_MazeOfHeck.

You cannot use a function in an if statement. Instead of using

if part.Touched:Connect then
...

Use

Part.Touched:Connect(function(hit)
    if Player.Team == Team then
        — player is on correct team add rest of your code here
    end
end

This new code fires when the part is touched and then checks if the player is on said team.

2 Likes