I’m trying to disconnect a changed event but I can’t get it to work.
Code:
script.Parent.Moving.Changed:Connect(function(Model)
if Check then
Check:Disconnect()
end
if Model ~= nil then
Check = function()
local dragDetector = Model["DragDetector"]
local movablePart = Model:FindFirstChild("HitBox")
local startPartPosition = nil
dragDetector.DragStart:Connect(function(player)
print("ok")
startPartPosition = movablePart.Position
end)
dragDetector.DragEnd:Connect(function(player)
startPartPosition = nil
end)
dragDetector:AddConstraintFunction(2, function(proposedMotion)
return constraintExampleUtils.roundToWorldGrid(proposedMotion, startPartPosition)
end)
dragDetector.DragContinue:Connect(function()
local Bad = {}
for _, Touching in pairs(movablePart:GetTouchingParts()) do
if Touching.Name == "HitBox" then
table.insert(Bad,Touching)
end
end
if #Bad > 0 then
movablePart:FindFirstChildWhichIsA("Texture").Color3 = Color3.new(255,0,0)
else
movablePart:FindFirstChildWhichIsA("Texture").Color3 = Color3.new(0,255,0)
end
end)
end
end
end)
local constraintExampleUtils = require(game.ReplicatedStorage.Library.Modules.ConstraintExampleUtils)
local Check1
local Check2
local Check3
local Check4
local going = false
local startPartPosition = nil
script.Parent.Moving.Changed:Connect(function(Model)
if Check1 then
Check1:Disconnect()
Check2:Disconnect()
Check3:Disconnect()
Check4:Disconnect()
end
if Model ~= nil then
local dragDetector = Model["DragDetector"]
local movablePart = Model:FindFirstChild("HitBox")
Check1 = dragDetector.DragStart:Connect(function(player)
print("ok")
startPartPosition = movablePart.Position
end)
Check2 = dragDetector.DragEnd:Connect(function(player)
startPartPosition = nil
end)
Check3 = dragDetector:AddConstraintFunction(2, function(proposedMotion)
return constraintExampleUtils.roundToWorldGrid(proposedMotion, startPartPosition)
end)
Check4 = dragDetector.DragContinue:Connect(function()
local Bad = {}
for _, Touching in pairs(movablePart:GetTouchingParts()) do
if Touching.Name == "HitBox" then
table.insert(Bad,Touching)
end
end
if #Bad > 0 then
movablePart:FindFirstChildWhichIsA("Texture").Color3 = Color3.new(255,0,0)
else
movablePart:FindFirstChildWhichIsA("Texture").Color3 = Color3.new(0,255,0)
end
end)
end
end)