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
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.
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
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)
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â
it is working thank you so much !
yeah im new to this i will take a look to the introduction thanks
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.