Touch only activate when every players touched it

Hello

im trying to script a touch event that works only when all the players touched it atleast one time
but i dont have the capacity for that

Basically, you’re going to have to keep a list of every player that touches it and compare that to the player list every time someone touches it. Once it matches the player list you can call another function because now you know every player has touched it.

Here is a simple example, though note I’m not cleaning anything out of the table if players leave or something so you might end up with some extra useless data

local foundPlayers = {}

local function allPlayersTouched()
    --Put the code you want to run after every player has touched it here.
    foundPlayers = {} --This resets it so every player needs to touch it again.  Remove if that's not what you want.  Otherwise keep it inside this function in a place it will run.
end

local function compareToPlayers()
    for _, plr in pairs(game.Players:GetPlayers()) do
        if not foundPlayers[plr] then
            return false
        end
    end
    return true
end

local function onTouch(part)
    local character = part:FindFirstAncestorOfClass("Model")
    if not character then return end

    local player = game.Players:GetPlayerFromCharacter(character)
    if not player then return end
    
    foundPlayers[player] = true
    if compareToPlayers() then
        allPlayersTouched()
    end
end

I didn’t test this. It’s very likely there is a typo or I misremembered a functions name. But this should technically work.

2 Likes

What am i supposed to replace the “part” with ?

i did put a normal script in the part that i want to be touched and wrote “local part = script.parent”
but it seems to not work
image

Part is supplied automatically by the touched event.

game.Workspace.PartPlayersTouch.Touched:Connect(onTouch)

Ahh, just realized that was a typo and I forgot to include the ‘function’ keyword

I dont have errors anymore but it seems that i did that wrong
Nothing is printing when 2 players touch it

local foundPlayers = {}

game.Workspace.AllP.Touched:Connect(function(onTouch)
	local function allPlayersTouched()
		print("everyone has touched it")
		foundPlayers = {}
	end

	local function compareToPlayers()
		for _, plr in pairs(game.Players:GetPlayers()) do
			if not foundPlayers[plr] then
				return false
			end
		end
		return true
	end

	local function onTouch(part)
		local character = part:FindFirstAncestorOfClass("Model")
		if not character then return end

		local player = game.Players:GetPlayerFromCharacter(character)
		if not player then return end

		foundPlayers[player] = true
		if compareToPlayers() then
			allPlayersTouched()
		end
	end
end)

You shouldn’t put it all inside a function.

You should do this

local foundPlayers = {}

local function allPlayersTouched()
	print("everyone has touched it")
	foundPlayers = {}
end

local function compareToPlayers()
	for _, plr in pairs(game.Players:GetPlayers()) do
		if not foundPlayers[plr] then
			return false
		end
	end
	return true
end

local function onTouch(part)
	local character = part:FindFirstAncestorOfClass("Model")
	if not character then return end

	local player = game.Players:GetPlayerFromCharacter(character)
	if not player then return end

	foundPlayers[player] = true
	if compareToPlayers() then
		allPlayersTouched()
	end
end
game.Workspace.AllP.Touched:Connect(onTouch)
1 Like

You’re not supposed to put all of the functions in the Touched connection. If you’re struggling with simplest scripts, you might want to start with Roblox scripting intro and maybe some LUA basics too.

“Give a man a fish, teach a man to fish”

1 Like

it is working thank you so much !

1 Like

yeah im new to this i will take a look to the introduction thanks

1 Like

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