Why is .Touched randomly firing?

I have a script for a slide mechanic, which spawns in a part called “Detector” used to check if a player is about to hit a wall, and cancels their slide if .Touch fires to achieve this. The part is positioned at the same place as the HumanoidRootPart by setting the CFrame on RenderStepped, and the rotation is set as the rotation of the player right as the slide began.

For some reason however, roughly one and a half seconds into each slide, the touched event suddenly fires, claiming the detector part touched whatever part was directly below it (i.e. baseplate) even though it is always at the midpoint of the player.

I didn’t have this issue when positioning the detector using a weld, but I need to lock the rotation of the part, so I can’t use welds to solve this issue.

In case anything I’ve said here is not clear enough, here is the full script:

local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")

local player = Players.LocalPlayer
local cam = workspace.CurrentCamera
local campos = CFrame.new(0, 0, 0)

local hasdetector = false
local saverot = nil

local function playAnim(Id)
	RS.Change3rdPersonAnim:FireServer(player.Name, Id)
end

local function stopAnim()
	RS.EndPlrAnims:FireServer(player.Name)
end

player.CharacterAdded:Connect(function()
	
	local char = player.Character or player.CharacterAdded:Wait()
	
	UIS.InputBegan:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.C then
			if char.Humanoid:GetState() == Enum.HumanoidStateType.Running then

				char.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)

				local slide = Instance.new("LinearVelocity", char.HumanoidRootPart)
				slide.MaxForce = math.huge 
				slide.VectorVelocity = char.HumanoidRootPart.CFrame.LookVector*50
				slide.Attachment0 = char.HumanoidRootPart.RootAttachment

				local slidestatus = Instance.new("StringValue")
				slidestatus.Name = "Slide"
				slidestatus.Parent = char

				local detector = Instance.new("Part")
				detector.Parent = char
				detector.Name = "Detector"
				detector.Size = Vector3.new(1, 1, 3)
				detector.CanCollide = false
				detector.CFrame = char.HumanoidRootPart.CFrame
				detector.Transparency = 0
				
				saverot = char.HumanoidRootPart.Orientation
				
				hasdetector = true

				campos = CFrame.new(0, -2, 0)
				playAnim("rbxassetid://13130585812")

				detector.Touched:Connect(function(part)
					if part.Parent ~= char and part.Parent:IsA("Accessory") == false and not string.find(part.Parent.Name, "VM") then
						print(part)
						slide:Destroy()
						detector:Destroy()
						slidestatus:Destroy()
						
						hasdetector = false
						
						char.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
						campos = CFrame.new(0, 0, 0)
						
						stopAnim()
					end
				end)

				char.Humanoid.StateChanged:Connect(function(state)
					if state ~= Enum.HumanoidStateType.Running or state ~= Enum.HumanoidStateType.Landed then
						slide:Destroy()
						detector:Destroy()
						slidestatus:Destroy()
						
						hasdetector = false

						char.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
						campos = CFrame.new(0, 0, 0)
						
						stopAnim()
					end
				end)

			end
		end
	end)

	UIS.InputEnded:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.C then
			if char.HumanoidRootPart:FindFirstChildWhichIsA("LinearVelocity") then
				char.HumanoidRootPart:FindFirstChildWhichIsA("LinearVelocity"):Destroy()
				char:FindFirstChild("Detector"):Destroy()
				char:FindFirstChild("Slide"):Destroy()
				
				hasdetector = false

				char.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
				campos = CFrame.new(0, 0, 0)
				
				stopAnim()
			end
		end
	end)
end)

RunService.RenderStepped:Connect(function()
	cam.CFrame *= campos
	if hasdetector == true then
		player.Character.Detector.CFrame = CFrame.new(player.Character.HumanoidRootPart.Position)*CFrame.Angles(math.rad(saverot.X), math.rad(saverot.Y), math.rad(saverot.Z))
	end
end)
1 Like

try to make the detector Anchored.

1 Like

Anchored parts won’t ever fire the .Touched event since it only runs for physically simulated parts.

1 Like

Solved by just switching to using raycasts. For anyone wondering, this is the functioning code:

local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")

local player = Players.LocalPlayer
local cam = workspace.CurrentCamera
local campos = CFrame.new(0, 0, 0)

local sliding = false
local saverot = nil

local connection

local function playAnim(Id)
	RS.Change3rdPersonAnim:FireServer(player.Name, Id)
end

local function stopAnim()
	RS.EndPlrAnims:FireServer(player.Name)
end

local function destroySlide()
	player.Character.HumanoidRootPart:FindFirstChildWhichIsA("LinearVelocity"):Destroy()
	player.Character:FindFirstChild("Slide"):Destroy()

	sliding = false

	player.Character.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, true)
	campos = CFrame.new(0, 0, 0)

	stopAnim()
	connection:Disconnect()
end

player.CharacterAdded:Connect(function()
	
	local char = player.Character or player.CharacterAdded:Wait()
	
	UIS.InputBegan:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.C then
			if char.Humanoid:GetState() == Enum.HumanoidStateType.Running then

				char.Humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)

				local slide = Instance.new("LinearVelocity", char.HumanoidRootPart)
				slide.MaxForce = math.huge 
				slide.VectorVelocity = char.HumanoidRootPart.CFrame.LookVector*50
				slide.Attachment0 = char.HumanoidRootPart.RootAttachment

				local slidestatus = Instance.new("StringValue")
				slidestatus.Name = "Slide"
				slidestatus.Parent = char
				
				saverot = char.HumanoidRootPart.CFrame.LookVector
				
				sliding = true

				campos = CFrame.new(0, -2, 0)
				playAnim("rbxassetid://13130585812")
				
				connection = char.Humanoid.StateChanged:Connect(function(state)
					if state ~= Enum.HumanoidStateType.Running or state ~= Enum.HumanoidStateType.Landed and sliding == true then
						destroySlide()
						connection:Disconnect()
					end
				end)

			end
		end
	end)

	UIS.InputEnded:Connect(function(input)
		if input.KeyCode == Enum.KeyCode.C then
			if char.HumanoidRootPart:FindFirstChildWhichIsA("LinearVelocity") then
				destroySlide()
			end
		end
	end)
end)

local rayParams = RaycastParams.new()
rayParams.FilterType = Enum.RaycastFilterType.Include
rayParams.FilterDescendantsInstances = {workspace.Map}

RunService.RenderStepped:Connect(function()
	cam.CFrame *= campos
	if sliding == true then
		local ray = workspace:Raycast(player.Character.HumanoidRootPart.Position+Vector3.new(0, -2, 0), saverot*5000, rayParams)
		if ray then
			if ray.Distance <= 5 then
				destroySlide()
			end
		end
	end
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.