Can Someone Help With My Touch Script

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I want to make a part where players touch and only 2 players will be shown a GUI. The problem is I’m not sure how to do this, I’ve tried using debounces so no more players can touch it. Now I’m thinking of adding players to a table instead.

  1. What is the issue? Include screenshots / videos if possible!

I want to get the first player who touches the part and then add their name to the “captains” table.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

Tried looking online for resources including the dev forum but couldn’t find anything to help me.

Here is my code thanks.

-- This is an example Lua code block

local captains = {}

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then

		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		
		

	end
end)

Maybe try BasePart | Roblox Creator Documentation

After you get your player with what you have right now, check if there is still room in captains by checking its length, if its less than two, insert with table.insert:

if #captains < 2 then
    table.insert(captains,player)
end

You might also want to check if the player is already in the table, so also add that as a condition by using table.find to see if there is already an index that points to the player:

if #captains < 2 and not table.find(captains,player) then
    table.insert(captains,player)
end
1 Like

How would I use it in a script?

Thank you for the help it worked.

1 Like