I am creating a slightly complex system for a horror game, it has two parts, both have a touch detection system and when you interact with one, the other block has it’s CanTouch function disabled until the other has finished it’s script. I’d like to point out that my knowledge in Lua is rather limited and I am unable to make this script profesionally by myself, which is most likely the reason for these bugs popping up.
I will show the view of the model from the Explorer for reference:
The problem is, sometimes when you interact with the model (sometimes by touching both of them in a short time frame, sometimes just by interacting with CeilingPanel without touching the other), a glitch occurs and the model stops working completely, it is a rare occurance but I need to make this function fully without any errors to progress in the development of the game. This is what the Developer Console shows when that error occurs:
For further reference, this is the EffectScript:
script.Parent.CanTouch = false
wait(0.1)
script.Parent.CanTouch = true
local script = script.Parent:WaitForChild("EffectScript")
local Part = script.Parent
Part.Touched:Connect(function(h)
script.Parent.CanTouch = false
script.Parent.Parent.Detector.CanTouch = false
script.Parent.Parent.Detector.Respawn.Disabled = true
if h and h.Parent:FindFirstChild("Humanoid") then
end
local number1 = math.random(1,10)
if number1 <= 1 then
script.Parent.CanTouch = false
script.Parent.Parent.Detector.CanTouch = false
script.Parent.Step:Play()
script.Parent.Dust.Enabled = true
script.Parent.Particles.Enabled = true
script.Parent.Smoke.Enabled = true
wait(0.1)
script.Parent.Particles.Enabled = false
script.Parent.Smoke.Enabled = false
script.Parent.Collapse:Play()
script.Parent.Transparency = 1
script.Parent.CanCollide = false
script.Parent.TextureTop.Transparency = 1
script.Parent.TextureBottom1.Transparency = 1
script.Parent.TextureBottom2.Transparency = 1
script.Parent.Debris.Enabled = true
wait(0.2)
script.Parent.Debris.Enabled = false
script.Parent.Dust.Enabled = false
local number2 = math.random(1,5)
if number2 == 1 then
wait(10)
end
if number2 == 2 then
wait(10)
end
if number2 == 3 then
wait(10)
end
if number2 == 4 then
wait(10)
end
if number2 == 5 then
wait(10)
end
script.Parent.Glitch.Enabled = true
script.Parent.Transparency = 0
script.Parent.CanCollide = true
script.Parent.Respawn:Play()
wait(0.5)
script.Parent.Glitch.Enabled = false
script.Parent.TextureTop.Transparency = 0
script.Parent.TextureBottom1.Transparency = 0
script.Parent.TextureBottom2.Transparency = 0
wait(0.5)
script.Parent.CanTouch = true
script.Parent.Parent.Detector.CanTouch = true
script.Parent.Parent.Detector.Respawn.Disabled = false
else
script.Parent.CanTouch = false
script.Parent.Parent.Detector.CanTouch = false
script.Parent.Step:Play()
script.Parent.Dust.Enabled = true
script.Parent.Smoke.Enabled = true
script.Parent.Particles.Enabled = true
wait(0.1)
script.Parent.Dust.Enabled = false
script.Parent.Smoke.Enabled = false
script.Parent.Particles.Enabled = false
repeat wait() until game.Players:GetPlayerFromCharacter(h.Parent):DistanceFromCharacter(script.Parent.Position) > 10
script.Parent.CanTouch = true
script.Parent.Parent.Detector.CanTouch = true
script.Parent.Parent.Detector.Respawn.Disabled = false
end
end)
Here is the Respawn script:
script.Parent.CanTouch = false
wait (0.1)
script.Parent.CanTouch = true
model = script.Parent.Parent.CeilingPanel
backup = model:clone()
enabled = true
function regenerate()
script.Parent.CanTouch = false
script.Parent.Parent.CeilingPanel.EffectScript.Disabled = true
script.Parent.Parent.CeilingPanel.CanCollide = false
script.Parent.Parent.CeilingPanel.Anchored = false
wait(0.05)
script.Parent.Parent.CeilingPanel.EffectScript.Disabled = true
script.Parent.Parent.CeilingPanel.CanCollide = true
script.Parent.Parent.CeilingPanel.GravityRunner.Disabled = false
script.Parent.Parent.CeilingPanel.Dust.Enabled = false
script.Parent.Parent.CeilingPanel.Particles.Enabled = false
script.Parent.Parent.CeilingPanel.Smoke.Enabled = false
script.Parent.Parent.CeilingPanel.Debris.Enabled = false
local number2 = math.random(1,6)
if number2 == 1 then
wait(15)
end
if number2 == 2 then
wait(16)
end
if number2 == 3 then
wait(17.5)
end
if number2 == 4 then
wait(20)
end
if number2 == 5 then
wait(21)
end
if number2 == 6 then
wait(22.5)
end
script.Parent.Parent.CeilingPanel.BrickColor = BrickColor.new(0,0,0)
script.Parent.Parent.CeilingPanel.Material = ("Neon")
script.Parent.Parent.CeilingPanel.Respawn:Play()
script.Parent.Parent.CeilingPanel.Glitch.Enabled = true
wait(0.5)
script.Parent.Parent.CeilingPanel.Glitch.Enabled = false
script.Parent.Parent.CeilingPanel.Transparency = 1
wait(1)
model:remove()
model = backup:clone()
model.Parent = script.Parent.Parent
model:makeJoints()
end
function onHit(hit)
if (hit.Parent:FindFirstChild("Humanoid") ~= nil) and enabled then
script.Parent.CanTouch = false
script.Parent.Parent.CeilingPanel.CanTouch = false
regenerate()
wait(0.5)
script.Parent.CanTouch = true
end
end
script.Parent.Touched:connect(onHit)
The “GravityRunner” script just makes the CeilingPanel have no gravity so it floats in the air, for visual purposes.
In short, the Respawn script makes you unable to interact with the CeilingPanel for a special event to happen, before the CeilingPanel is respawned for the whole thing to repeat again when triggered. I am thankful for all help!