My Team Only ProximityPrompt Script is not Working!

  1. Whats the goal:
    a. The goal is to make the gate Open/Close When a Player On One of the Teams Listed in the teams allowed Table to Open and close after the set time within the script all while on a Proximity Prompt

  2. What is the issue:
    a. The issue is I scripted everything, but Im not getting any errors but it is not working I know its not the most pretty script but I was trying to solve the problem and it clearly didnt work…

local gate = script.Parent.Parent
local prompt = script.Parent.ProximityPrompt
local TweenService = game:GetService("TweenService")
local status = "Closed"
local Teams = game:GetService("Teams")
local Closed1 = gate.Closed.MeshPart
local Closed2 = gate.Closed.Part
local Opened1 = gate.Opened.MeshPart
local Opened2 = gate.Opened.Parent

local teamsallowed = {
	"Tigard Police Department"
}


prompt.Triggered:Connect(function(plr)
	if plr.Team == table.find(teamsallowed, tostring(plr.Team)) then
		if status == "Closed" then
			Opened1.Transparency = 1
			Closed1.Transparency = 0
			Opened1.CanCollide = false
			Closed1.CanCollide = true
			Opened2.Transparency = 1
			Closed2.Transparency = 0
			Opened2.CanCollide = false
			Closed2.CanCollide = true
		elseif status == "Opened" then
			Opened1.Transparency = 0
			Closed1.Transparency = 1
			Opened1.CanCollide = true
			Closed1.CanCollide = false		
			Opened2.Transparency = 0
			Closed2.Transparency = 1
			Opened2.CanCollide = true
			Closed2.CanCollide = false
		end
	end
end)

In this line you are comparing the player’s team with a number.

table.find will return the index of where “Tigard Police Department” is in table, and plr.Team is “Tigard Police Department”. It ends up as:

if "Tigard Police Department" == 1 then

1 Like

Like what @Dev_Peashie has said above, the main issue here is that you are attempting to compare an instance with an index number.
I believe a fix to this is to use the team’s name rather than the instance (if you want to compare with instance, list the team’s location in the teamsallowed table), and use table.find again in the second argument within the function.

Fixed Code:

local teamsallowed = {
	"Tigard Police Department"
}


prompt.Triggered:Connect(function(plr)
	if plr.Team.Name == table.find(teamsallowed, table.find(plr.Team.Name)) then
		if status == "Closed" then
			Opened1.Transparency = 1
			Closed1.Transparency = 0
			Opened1.CanCollide = false
			Closed1.CanCollide = true
			Opened2.Transparency = 1
			Closed2.Transparency = 0
			Opened2.CanCollide = false
			Closed2.CanCollide = true
		elseif status == "Opened" then
			Opened1.Transparency = 0
			Closed1.Transparency = 1
			Opened1.CanCollide = true
			Closed1.CanCollide = false		
			Opened2.Transparency = 0
			Closed2.Transparency = 1
			Opened2.CanCollide = true
			Closed2.CanCollide = false
		end
	end
end)

Another way to check if the player is on the desired team is to use a for loop.

local teamsallowed = {
	"Tigard Police Department"
}


prompt.Triggered:Connect(function(plr)
	for index, teamNames in pairs(teamsallowed) do
		if plr.Team.Name == teamNames then
			if status == "Closed" then
				Opened1.Transparency = 1
				Closed1.Transparency = 0
				Opened1.CanCollide = false
				Closed1.CanCollide = true
				Opened2.Transparency = 1
				Closed2.Transparency = 0
				Opened2.CanCollide = false
				Closed2.CanCollide = true
			elseif status == "Opened" then
				Opened1.Transparency = 0
				Closed1.Transparency = 1
				Opened1.CanCollide = true
				Closed1.CanCollide = false		
				Opened2.Transparency = 0
				Closed2.Transparency = 1
				Opened2.CanCollide = true
				Closed2.CanCollide = false
			end
		end
	end
end)
1 Like

I dont think that would work, why double table.find?
With only one check is enough:

local teamsallowed = {
	"Tigard Police Department"
}

prompt.Triggered:Connect(function(plr)
	if table.find(teamsallowed, plr.Team.Name) then -- just one find, the team name
		if status == "Closed" then
			Opened1.Transparency = 1
			Closed1.Transparency = 0
			Opened1.CanCollide = false
			Closed1.CanCollide = true
			Opened2.Transparency = 1
			Closed2.Transparency = 0
			Opened2.CanCollide = false
			Closed2.CanCollide = true
		elseif status == "Opened" then
			Opened1.Transparency = 0
			Closed1.Transparency = 1
			Opened1.CanCollide = true
			Closed1.CanCollide = false		
			Opened2.Transparency = 0
			Closed2.Transparency = 1
			Opened2.CanCollide = true
			Closed2.CanCollide = false
		end
	end
end)

Honestly I think thats a bad idea, a loop just to find something in table?


Additionally I would prefer to do it like this, with a dictionary, its more neat:

-- Now this is a dictionary, not a table
local teamsallowed = {
	["Tigard Police Department"] = true
}

prompt.Triggered:Connect(function(plr)
	if teamsallowed[plr.Team.Name] then -- checking on dictionary
		if status == "Closed" then
			-- code
		elseif status == "Opened" then
			-- code
		end
	end
end)
1 Like

I used your Version on the script, but Im guessing it doesnt know if it is open or closed when you first load in, Because The Code that changes transparency and Collide still doesnt work…

local gate = script.Parent.Parent
local prompt = script.Parent.ProximityPrompt
local TweenService = game:GetService("TweenService")
local status = "Closed"
local Teams = game:GetService("Teams")
local Closed1 = gate.C1
local Closed2 = gate.C2
local Opened1 = gate.O1
local Opened2 = gate.O2


local teamsallowed = {
	"Tigard Police Department"
}

prompt.Triggered:Connect(function(plr)
	if table.find(teamsallowed, plr.Team.Name) then -- just one find, the team name
		if status == "Closed" then
			Opened1.Transparency = 1
			Closed1.Transparency = 0.02
			Opened1.CanCollide = false
			Closed1.CanCollide = true
			Opened2.Transparency = 1
			Closed2.Transparency = 0.1
			Opened2.CanCollide = false
			Closed2.CanCollide = true
		elseif status == "Opened" then
			Opened1.Transparency = 0.02
			Closed1.Transparency = 1
			Opened1.CanCollide = true
			Closed1.CanCollide = false		
			Opened2.Transparency = 0.1
			Closed2.Transparency = 1
			Opened2.CanCollide = true
			Closed2.CanCollide = false
		end
	end
end)

When you first load in? What do you mean. I guess this is a ServerScript, so its not taking in count a specific client, when scripts runs by first time “status” variable is “Closed” (idk if it matters but do not use the word “status” cause it was a global from roblox engine)

Anyway, when the script runs by first time it sets “status” variable to “Closed”, so when player triggers the prompt it only checks if that variable is equal to “Closed” or “Opened”.
Add prints to see whats going on when player triggers the prompt

I just ran Prints in the script, and It is not Opening the gate it is only Closing it and running the close script.

I switched the Names Around and now when I trigger the Prox it opens but it will stay open and doesnt close if it is open.

local gate = script.Parent.Parent
local prompt = script.Parent.ProximityPrompt
local TweenService = game:GetService("TweenService")
local status = "Closed"
local Teams = game:GetService("Teams")
local Closed1 = gate.C1
local Closed2 = gate.C2
local Opened1 = gate.O1
local Opened2 = gate.O2


local teamsallowed = {
	"Tigard Police Department"
}

prompt.Triggered:Connect(function(plr)
	if table.find(teamsallowed, plr.Team.Name) then -- just one find, the team name
		if status == "Opened" then
			Opened1.Transparency = 1
			Closed1.Transparency = 0.02
			Opened1.CanCollide = false
			Closed1.CanCollide = true
			Opened2.Transparency = 1
			Closed2.Transparency = 0.1
			Opened2.CanCollide = false
			Closed2.CanCollide = true
			print("Gate Closed")
		elseif status == "Closed" then
			Opened1.Transparency = 0.02
			Closed1.Transparency = 1
			Opened1.CanCollide = true
			Closed1.CanCollide = false		
			Opened2.Transparency = 0.1
			Closed2.Transparency = 1
			Opened2.CanCollide = true
			Closed2.CanCollide = false
			print("Gate Opened")
		end
	end
end)

I believe this is because there is nowhere in the script that is changing the status variable.

Here’s an implementation of setting the current status of the gates

local gate = script.Parent.Parent
local prompt = script.Parent.ProximityPrompt
local TweenService = game:GetService("TweenService")
local status = "Closed"
local Teams = game:GetService("Teams")
local Closed1 = gate.C1
local Closed2 = gate.C2
local Opened1 = gate.O1
local Opened2 = gate.O2


local teamsallowed = {
	"Tigard Police Department"
}

prompt.Triggered:Connect(function(plr)
	if table.find(teamsallowed, plr.Team.Name) then
		if status == "Opened" then
			Opened1.Transparency = 1
			Closed1.Transparency = 0.02
			Opened1.CanCollide = false
			Closed1.CanCollide = true
			Opened2.Transparency = 1
			Closed2.Transparency = 0.1
			Opened2.CanCollide = false
			Closed2.CanCollide = true

			status = "Closed"
			print("Gate ".. status)

		elseif status == "Closed" then
			Opened1.Transparency = 0.02
			Closed1.Transparency = 1
			Opened1.CanCollide = true
			Closed1.CanCollide = false		
			Opened2.Transparency = 0.1
			Closed2.Transparency = 1
			Opened2.CanCollide = true
			Closed2.CanCollide = false

			status = "Opened"
			print("Gate "..status)
		end
	end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.