Ok,so I was bored and decided to copy Stickmasterlukes's button script and rewrote it myself in order to learn new functions.
The problem with this script is that if you stand on the Part for a few seconds and leave it won't fire the function UnTouched properly(It will just stay green),why? Oh,and I don't know what Line 27 is and its purpose( is it a for loop with two variables assigned?).
CurrentlyHitting = {}
------Vairibles------
Part = script.Parent
On = BrickColor.new("Bright green")
Off = BrickColor.new("Bright red")
------Functions-----
function FindInTable(Table,Item)--Finds items in a table
for Number,ItemInTable in pairs (CurrentlyHitting) do--Loops to get all of the objects
if Item ==ItemInTable--If it finds the object with the excat name return it
then
return Item,Number
end
end
end
----------------------
function OnTouch(Hit)-- For the Hit Event
if Hit then--It hit something!
if not FindInTable(CurrentlyHitting.Hit)--Finds if the that it just hit is on the table,if not then do the following
then
table.insert(CurrentlyHitting,Hit)--Inserts it in the table
Part.BrickColor = On
end
end
end
----------------------
function UnTouched(UnHit)--For TouchEnded Event
local Item,ItemNumber = FindInTable(CurrentlyHitting,UnHit)
if Item and ItemNumber
then
table.remove(CurrentlyHitting,Itemnumber)--Removes it off the Table
if #CurrentlyHitting == 0--If the table is empty
then
Part.BrickColor = Off
end
end
end
----------------------
Part.BrickColor = Off
------Events-----
Part.Touched:connect(OnTouch)
Part.TouchEnded:connect(UnTouched)
