Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to create a queue pad? Made a script, very broken.

Hi, I am trying to make a queue pad(Like in games like "The Purge" etc) I used simple functions like Touched and TouchEnded, but it is so buggy.

My main problem is that when a player is standing on the pad, it keeps removing them from the table and then adding them back. Also, if you step off sometimes, it doesn't remove you.

I just want to know if there are any changes or things I can add to fix those problems.

_G.waitinginline = {}

local countdownon = false



function tablecontains(table, element)
  for _, value in pairs(table) do
    if value == element then
      return true
    end
  end
  return false
end


function tablefind(tab,el)
    for index, value in pairs(tab) do
        if value == el then
            return index
        end
    end
end

function showplayer(player)
	local clone = game.Lighting.Character:Clone()
	clone.Name = player.Name
	clone.Namee.Text = player.Name
	clone.Frame.ImageLabel.Image = "http://www.roblox.com/Thumbs/Avatar.ashx?x=420&y=420&username="..player.Name
	clone.Parent = game.Workspace.InLine.SurfaceGui.PlayersInLine
end

function removeplayer(player)
	if game.Workspace.InLine.SurfaceGui.PlayersInLine:FindFirstChild(player.Name) then
	game.Workspace.InLine.SurfaceGui.PlayersInLine:FindFirstChild(player.Name):Destroy()
	end
end

script.Parent.Touched:connect(function(hitt)
	local hit = game.Players:GetPlayerFromCharacter(hitt.Parent)
	if #_G.waitinginline < 10 then
	if hitt.Name == "Torso" then
	if tablecontains(_G.waitinginline, hit) then
	else
		table.insert(_G.waitinginline, hit)
				showplayer(hit)
				game.ReplicatedStorage.InLine.Value = #_G.waitinginline
	end
	end
	end
end)

script.Parent.TouchEnded:connect(function(hitt)
	local hit = game.Players:GetPlayerFromCharacter(hitt.Parent)
	if hitt.Name == "Torso" then
if tablecontains(_G.waitinginline, hit) then
	table.remove(_G.waitinginline, tablefind(_G.waitinginline, hit))
	removeplayer(hit)
	game.ReplicatedStorage.InLine.Value = #_G.waitinginline
	else
end
end
end)

:D

1 answer

✓ Accepted answer

Instead of storing a table that adds or removes players when they enter/leave the pad, you can instead create a Region3 around the pad when you want to check for the players. This will eliminate glitchiness from .Touched and allow you to get efficient and accurate detection of characters.

If you made an invisible CanCollide false part in a cube around the pad called Detect, you could do something like this:

local DetectPart = workspace.Detect

local function CreateRegion3FromLocAndSize(Position, Size)
	local SizeOffset = Size/2
	local Point1 = Position - SizeOffset
	local Point2 = Position + SizeOffset
	return Region3.new(Point1, Point2)
end

local function GetPlayersInRegion(Box)
	local Plrs = {}
	local PlrsDetected = {}
	local Reg = CreateRegion3FromLocAndSize(Box.Position, Box.Size)
	for _,Part in pairs(workspace:FindPartsInRegion3(Reg,nil,math.huge) do
		local Plr = game.Players:GetPlayerFromCharacter(Part.Parent)
		if Plr and not PlrsDetected[Plr] then --if detects a character that hasn't already been detected
			table.insert(Plrs,Plr)
			PlrsDetected[Plr] = true
		end
	end
	return Plrs
end

local function IsPlayerInRegion(Player)
	local PlayersInRegion = GetPlayersInRegion(DetectPart)
	for _,Plr in pairs(PlayersInRegion) do
		if Plr == Player then
			return true
		end
	end
	return false
end

The CreateRegion3FromLocAndSize function is from the wiki. Also for future reference, try to avoid _G as much as possible. If you want to share the information between scripts, consider using ModuleScripts or BindableEvents/Functions.

3 comments

0

How would I detect when the player is not in that area?

0

Iterate through the players in the region and check if the player isn't one of them. Edited the script to add a function that checks.

0

just wondering how would i make this so if a specific amount of players or plrs were detected in that region it runs a function or runs a piece of code? im very new to scripting and im slowly learning this

This discussion is preserved as it was. New answers, comments, and votes are closed.