Hey all,
I am trying to make some kind of gate which has to check a couple of things when a player touches a part.
It should only do the stuff if the player is…
sitting
in one of a variable amount of teams
This is my script at the moment, it doesn’t really seem to do what I need it to do. I tried using a table but couldn’t find a way to make it work…
local AllowedTeams = {
"Team1";
"Team2";
}
Sensor.Touched:Connect(function(Thing)
if game.Players:GetPlayerFromCharacter(Thing.Parent) ~= nil and Thing.Parent:FindFirstChild("Humanoid").Sit == true then
print("Sit=true, Player found")
if game.Players:GetPlayerFromCharacter(Thing.Parent).Team.Name == AllowedTeams then
print("Player.Team=AllowedTeam")
-- Some stuff
end
end
end)
I feel like I got pretty close
Also, I don’t quite understand the ‘~= nil’ part (I just copied that from somebody else) EDIT: It stops after it prints the first sentence
Doing a comparison using a table will not work, instead, you should iterate through the table and compare whether the player is on of those teams.
I have amended your code to include this:
local AllowedTeams = {
"Team1",
"Team2"
}
Sensor.Touched:Connect(function(Thing)
-- ~= nil essentially means whether something exists, it checks whether game.Players:GetPlayerFromCharacter(Thing.Parent) exists
if game.Players:GetPlayerFromCharacter(Thing.Parent) ~= nil and Thing.Parent:FindFirstChild("Humanoid").Sit == true then
print("Sit=true, Player found")
for i, teamName in pairs(AllowedTeams) do
if game.Players:GetPlayerFromCharacter(Thing.Parent).Team.Name == teamName then
print("Player.Team=AllowedTeam")
-- Some stuff
-- Break out of the loop
break
end
end
end)
Ah that’s why! I tried replacing the code but now it doesn’t seam to print the first text anymore… Didn’t change anything EDIT: One bracket was missing, now it works perfectly!
Try this, you don’t need to loop through the table. Using table.find would work perfectly fine!
local Players = game:GetService("Players")
local AllowedTeams = {
"Team1";
"Team2";
}
Sensor.Touched:Connect(function(Thing)
if Thing.Parent:FindFirstChildOfClass("Humanoid") then
if Players:FindFirstChild(Thing.Parent.Name) then
local Player = Players:GetPlayerFromCharacter(Thing.Parent)
local Humanoid = Thing:FindFirstChildOfClass("Humanoid")
if Humanoid.Sit then
print("Sit=true, Player found")
end
if Player and table.find(AllowedTeams, Player.Team.Name) then
print("Player.Team=AllowedTeam")
end
end
end
end)
Thanks for the help, the other solution works for me but this really could come in handy later. Now I understand a bit more about tables.
Thanks for helping everyone!