.Touched requires you to move to work?

I’m making a script that requires the player to press W while touching a part, to teleport to someplace else, but I can’t do this, because for .Touched to work, the player needs to be moving, and moving and pressing W makes it hard to teleport to a position. Here’s my code:

local UIS = game:GetService("UserInputService")
local Level1Folder = game.Workspace:FindFirstChild("Level1")
local Level2Folder = game.Workspace:FindFirstChild("Level2")
local Level3Folder = game.Workspace:FindFirstChild("Level3")
local Level4Folder = game.Workspace:FindFirstChild("Level4")

local TPKey = Enum.KeyCode.W

Level1Folder.ExitPoint.Touched:Connect(function(partTouching)
	if partTouching.Parent:FindFirstChild("Humanoid") then
		if UIS:IsKeyDown(TPKey) == true then
			partTouching.Parent:SetPrimaryPartCFrame(CFrame.new(
				Level2Folder.EntryPoint.Position + Vector3.new(0, 10, 0)
				)
			)

		end	
	end
end)

Level2Folder.ExitPoint.Touched:Connect(function(partTouching)
	if partTouching.Parent:FindFirstChild("Humanoid") then
		if UIS:IsKeyDown(TPKey) == true then
			partTouching.Parent:SetPrimaryPartCFrame(CFrame.new(
				Level3Folder.EntryPoint.Position + Vector3.new(0, 10, 0)
				)
			)
		end	
	end
end)

Level3Folder.ExitPoint.Touched:Connect(function(partTouching)
	if partTouching.Parent:FindFirstChild("Humanoid") then
		if UIS:IsKeyDown(TPKey) == true then
			partTouching.Parent:SetPrimaryPartCFrame(CFrame.new(
				Level4Folder.EntryPoint.Position + Vector3.new(0, 10, 0)
				)
			)
		end	
	end
end)

I would avoid .Touched for this since it .Touched is not accurate. If you want to simply check if they are on top of a certain brick you can use a simple raycast downwards with a whitelist of the part and check if its there or you can use a region3 each time they press it. Region3 might be bad in terms of performance however so I recommend doing raycast each W key press.

1 Like

Have you tried GetTouchingParts()?

local primaryPart = script.Parent

for _, part in ipairs(primaryPart:GetTouchingParts()) do
    -- Code
end

And it would be preferable to check for the W key then check for touching parts so that you don’t waste performance by creating a loop.

It’s also important to note that GetTouchingParts returns all parts that have CanCollide enabled unless the aforementioned non-collideable part has a TouchInterest instance.


Here is the API reference for GetTouchingParts:() :

I think you can use this here too it’s quite simple