When I run on the part, my TouchEnded trigger runs. I’m still on the part. Please help!
game.Workspace.detector1.Touched:Connect(function()
if debounce == false then
print(debounce)
debounce = true
print("if loop running")
game.Workspace["A-Chassis 6C by Novena"]["A-Chassis Tune"]["A-Chassis Interface"].Detector1.Value = true
if game.Workspace["A-Chassis 6C by Novena"]["A-Chassis Tune"]["A-Chassis Interface"].Detector2.Value == true then
game.Workspace["A-Chassis 6C by Novena"]["A-Chassis Tune"]["A-Chassis Interface"].CanGo.Value = true
end
end
end)
game.Workspace.detector1.TouchEnded:Connect(function()
if debounce == true then
print(debounce)
debounce = false
print("if loop running")
game.Workspace["A-Chassis 6C by Novena"]["A-Chassis Tune"]["A-Chassis Interface"].Detector1.Value = false
if game.Workspace["A-Chassis 6C by Novena"]["A-Chassis Tune"]["A-Chassis Interface"].CanGo.Value == true then
game.Workspace["A-Chassis 6C by Novena"]["A-Chassis Tune"]["A-Chassis Interface"].CanGo.Value = false
end
end
end)
Instead of detecting the touch on any part of the character, focus on the HumanoidRootPart
. This part is less likely to cause false triggers.
-- Initialize debounce variable
local debounce = false
-- Function to handle part touched
local function onTouch()
if not debounce then
print(debounce)
debounce = true
print("if loop running")
game.Workspace["A-Chassis 6C by Novena"]["A-Chassis Tune"]["A-Chassis Interface"].Detector1.Value = true
if game.Workspace["A-Chassis 6C by Novena"]["A-Chassis Tune"]["A-Chassis Interface"].Detector2.Value == true then
game.Workspace["A-Chassis 6C by Novena"]["A-Chassis Tune"]["A-Chassis Interface"].CanGo.Value = true
end
end
end
-- Function to handle part touch ended
local function onTouchEnded()
if debounce then
print(debounce)
debounce = false
print("if loop running")
game.Workspace["A-Chassis 6C by Novena"]["A-Chassis Tune"]["A-Chassis Interface"].Detector1.Value = false
if game.Workspace["A-Chassis 6C by Novena"]["A-Chassis Tune"]["A-Chassis Interface"].CanGo.Value == true then
game.Workspace["A-Chassis 6C by Novena"]["A-Chassis Tune"]["A-Chassis Interface"].CanGo.Value = false
end
end
end
-- Connect touch events to functions
game.Workspace.detector1.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("HumanoidRootPart") then
onTouch()
end
end)
game.Workspace.detector1.TouchEnded:Connect(function(hit)
if hit.Parent:FindFirstChild("HumanoidRootPart") then
onTouchEnded()
end
end)
The same issue is occuring.
Do not focus on the HumanoidRootPart. It won’t activate when any other parts are touching.
Instead you want to run code for “touching” when any body part is touching and code for “not touching” when no body part is touching.
2 Likes
.Touched and .TouchEnded are really bad for detection and this sort of thing is just gonna happen, you’re best bet is using a different method for this
@batteryday has the right idea.
Here’s how I would implement it using your code structure
(I didn’t add all of your code, you’ll need to add that back in where my comments tell you to. I had to remove it since my workspace didn’t match yours causing errors.)
local debounce = false
local hitList = {} -- used to track which parts are touching detector
local Players = game:GetService("Players")
function playerHit() -- checks to see if a player is currently touching detector
for part, v in pairs(hitList) do
if Players:GetPlayerFromCharacter(part.Parent) then
return true
end
end
return false
end
game.Workspace.detector1.Touched:Connect(function(hit)
hitList[hit] = true
if playerHit() and not debounce then
debounce = true
-- run your code here
print("Yes") -- used for debug purpose
end
end)
game.Workspace.detector1.TouchEnded:Connect(function(hit)
hitList[hit] = nil
if not playerHit() and debounce then
debounce = false
-- run your code here
print("No") -- used for debug purpose
end
end)
local detector = game.Workspace.detector1
local touchingParts = {} -- Table to keep track of touching parts
local debounce = false
local function updateDetectorState()
local isTouching = next(touchingParts) ~= nil -- Check if there are any touching parts
game.Workspace["A-Chassis 6C by Novena"]["A-Chassis Tune"]["A-Chassis Interface"].Detector1.Value = isTouching
if isTouching and game.Workspace["A-Chassis 6C by Novena"]["A-Chassis Tune"]["A-Chassis Interface"].Detector2.Value == true then
game.Workspace["A-Chassis 6C by Novena"]["A-Chassis Tune"]["A-Chassis Interface"].CanGo.Value = true
elseif not isTouching then
game.Workspace["A-Chassis 6C by Novena"]["A-Chassis Tune"]["A-Chassis Interface"].CanGo.Value = false
end
debounce = isTouching
end
detector.Touched:Connect(function(hit)
local part = hit.Parent
if not touchingParts[part] then
touchingParts[part] = true
updateDetectorState()
end
end)
detector.TouchEnded:Connect(function(hit)
local part = hit.Parent
if touchingParts[part] then
touchingParts[part] = nil
updateDetectorState()
end
end)