I’m simply just trying to only allow a target:Touched event when target.Transparency == 0
This is essentially what the relevant code looks like, yet when target.Transparency is set to 1 the Touched event still fires…
local target = script.Parent
local function GiveXP(part)
-- Not showing entire script, just relevant parts...
...
target.Transparency = 1
wait(10)
target.Transparency = 0
end
if (target.Transparency == 0) then
target.Touched:Connect(GiveXP)
end
Basically… I’m trying to ensure the Touched event doesn’t keep firing when the part is not visible. I had the logic inside the GiveXP function, but it just seems expensive to have it keep firing when Touched and the part is not visible.
local target = script.Parent
local function GiveXP(part)
if (target.Transparency == 0) then
-- execute code here
target.Transparency = 1
wait(10)
target.Transparency = 0
end
end
target.Touched:Connect(GiveXP)
Doing that check everytime the part is touched shouldn’t cause performance problems. But here’s how you can prevent detecting touches when its not visible.
local touchedConn
local function GiveXP()
-- execute code here
target.Transparency = 1
wait(10)
target.Transparency = 0
end
local function transparencyChanged()
if target.Transparency == 0 then
touchedConn = target.Touched:Connect(GiveXP)
elseif touchedConn then
touchedConn:Disconnect()
touchedConn = nil
end
end
transparencyChanged()
target:GetPropertyChangedSignal("Transparency"):Connect(transparencyChanged)
This is exactly what I had originally, but I didn’t care for it as it fires even when the part is not visible… While I’m still learning LUA (I do “speak” other languages)… something like this (I feel) could be handled better. It may not cause performance issues overall, but I do still view it as expensive. Perhaps I’m just picky
local target = script.Parent
local function GiveXP(part)
target.Transparency = 1
wait(10)
target.Transparency = 0
end
local function touchFunc(target)
local conn
conn = target.Touched:Connect(function(hit)
conn:Disconnect()
GiveXP(hit)
touchFunc(target)
end)
end
touchFunc(target)