Touched script not working

Hey! I’ve been trying to make a boat system, but due to spam, I have tried to limit the amount of boats allowed in one area with this code. However, the ‘Touched’ function also detects players. I have created this code to combat this, but it still registers as touched. Could someone please tell me the problem?

local part = workspace.BoatCheckArea
local touching = 0

local function onPartTouched(otherPart)
	if not otherPart.Parent:FindFirstChildWhichIsA("Humanoid") then
		touching = 1
	end
end

local function onPartFinishedTouching(otherPart)
	if not otherPart.Parent:FindFirstChildWhichIsA("Humanoid") then
		touching = 0
	end
end

part.Touched:Connect(onPartTouched)
part.TouchEnded:Connect(onPartFinishedTouching)

Thanks!

Because it’ll count any touching parts, you need to check the touching part which resulted in the “.Touched” event firing.

2 Likes

This might be colliding with hats or other attachments on the Character that are not a direct child of the Character itself.

Try using the function game.Players:GetPlayerFromCharacter which I believe will accept any descendants of the Character.

If not, you’ll just need to adjust your logic to check otherPart.Parent.Parent

1 Like
local part = workspace.BoatCheckArea
local touching = 0

local function onPartTouched(otherPart)
	if otherPart.Parent:FindFirstChildWhichIsA("Humanoid") then
		
  else
           touching += 1
	end
end

local function onPartFinishedTouching(otherPart)
	if otherPart.Parent:FindFirstChildWhichIsA("Humanoid") then
		
else
touching = 0
	end
end

part.Touched:Connect(hit)
onPartTouched(hit)
end
part.TouchEnded:Connect(hit)
onPartFinishedTouching(hit)
end

Sorry for messy code

1 Like

Wow thanks, that was the problem.