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

touched and touchended functions are not working?

I attempted to make a part that only one player can touch and be added to a table, and when he untouches the part he gets removed from that table.

But my code didn't work and I don't know the reason

Here's my attempt:

local team1 = script.Parent.Team1

local maximum_1 = false

local team_table_1 = {}

  

local function onTouch(hit)

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

if not player then return end

if not maximum_1 then

maximum_1 = true

while true do

wait(hit)

table.insert(team_table_1, player.Name)

print'added'

end

end

end

local function onTouchEnded()

table.remove(team_table_1, 1)

print'removed'

end

team1.Part1.Touched:Connect(onTouch)

team1.Part1.TouchEnded:Connect(onTouchEnded)

1 answer

:Remove is deprecated and even using :Destroy you are only removing the element so it cant be detected again. What I would do is parent it in replicated storage then a wait() method and then add it back so it can detect it. Computers need time to think!

2 comments

0

Sorry but I don't understand, what should I do exactly? Can you explain and give an example in a sample of code?

0

Don't remove the table. Just set it to a new parent outside of the global scene. Table isnt a element. It is a method so you can store it as a variable which we call values. You can make a stringvalue then parent the string value to the replicated storage.

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