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
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)
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)
@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.
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.
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
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.