Touched event triggering even though A humanoid hasn't touched it?

I’m trying to make it so when you touch a part you get teleported and warned to not go there
But when Even though im not touching it. It still triggers.

Server

game.Players.PlayerAdded:Connect(function(player)
	local ExitValue = Instance.new("IntValue", player)
	ExitValue.Name = "ExitValue"
	while true do
		task.wait()
		if game.Workspace.TriggerParts.Preventer.Touched then
			
			local player = player
			local char = player.Character or player.CharacterAdded:Wait()
			local humanoid = char:WaitForChild("HumanoidRootPart")
			
			if humanoid then
			
			game.Workspace.TriggerParts.Preventer.CanTouch = false
			game.Workspace.TriggerParts.Preventer1.CanTouch = false
			game.Workspace.TriggerParts.Preventer2.CanTouch = false

			humanoid.CFrame = game.Workspace.SpawnCFRAME.CFrame
			ExitValue.Value = ExitValue.Value + 1

			game.ReplicatedStorage.RemoteEvents.Out:FireClient(player)
			
			
			wait(5)
			
			game.Workspace.TriggerParts.Preventer.CanTouch = true
			game.Workspace.TriggerParts.Preventer1.CanTouch = true
			game.Workspace.TriggerParts.Preventer2.CanTouch = true
				
			end
		end
	end
end)

Client

game.ReplicatedStorage.RemoteEvents.Out.OnClientEvent:Connect(function()
	if game.Players.LocalPlayer:WaitForChild("ExitValue").Value == 1 then
		game.Players.LocalPlayer.PlayerGui.ScreenGui.PreventText.TextTransparency = 0
		game.Players.LocalPlayer.PlayerGui.ScreenGui.TextLabel.TextTransparency = 1
		
		wait(2)
		
		game.Players.LocalPlayer.PlayerGui.ScreenGui.PreventText.TextTransparency = 1
		
	elseif game.Players.LocalPlayer:WaitForChild("ExitValue").Value == 2 then
		game.Players.LocalPlayer.PlayerGui.ScreenGui.PreventText.TextTransparency = 0
		game.Players.LocalPlayer.PlayerGui.ScreenGui.PreventText.Text = "I said don't go there"
		
		wait(2)
		
		game.Players.LocalPlayer.PlayerGui.ScreenGui.PreventText.TextTransparency = 1
		
	elseif game.Players.LocalPlayer:WaitForChild("ExitValue").Value == 3 then
		game.Players.LocalPlayer:Kick("I said don't do that!")
		
	
		
	end
end)
```.

“Touched” is an event, not a boolean (true/false).

“if game.Workspace.TriggerParts.Preventer.Touched then” will therefore always pass, as it’s actually checking whether or not the Touched event exists, which will always be the case for instances that inherit this event, such as parts.

You use events like this instead:

game.Workspace.TriggerParts.Preventer.Touched:Connect(function(part)
	print(part)-- prints the part that touched preventer
	-- you can check if a humanoid touched it by doing the following:
	if part.Parent:FindFirstChild("Humanoid") then

	end
end)

The contents of the function will run every time the part is touched, as long as the CanTouch property is set to true.

1 Like

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