While touch function

Hello, I need a help with the while true then clickdetector false.

I am making this for the train game and to protect from the troll which they will not be able to change rail points while train is on the point track. But as I tried, it doesn’t work for a long time and need a solution. If you have any ideas, please let me know. Thanks.
Code below…

script.Parent.Parent.Part.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
	if humanoid then
		script.Parent.ClickDetector.MaxActivationDistance = 0
	end
end)

script.Parent.Parent.Part.TouchEnded:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
	if humanoid then
		script.Parent.ClickDetector.MaxActivationDistance = 200
	end
end)

The script works fine. But it is only allowing you to click the ClickDetector if you’re not touching the part. Is that your intention? I believe you may have flipped the MaxActivationDistance for Touched and TouchEnded

This is the result for me right now using the script I posted. The script is located in the touching part. I changed it to

script.Parent.Touched:Connect(function(hit)

for the top
https://gyazo.com/a8f33259c9d441d7bdd62e84563dd23a

Didnt understand, can you add more information about what do you want?

Alright. So, in the video you see the green block right?
That green block has a clickdetector and is to change the rail side like you saw on the video.
Also, you see the white transparent block right. That is the block the player touches.
If the player touches that white transparent block, and while touching the green button clickdetector should be disabled. That’s the thing I want to do it.

Works fine for me. Do you have any errors? And did you turn off CanTouch by accident perhaps?


MiyusTrainThing.obj (3.1 KB)

1 Like

Just add a variable checking that it will probably work
because since you using R15 theres a bunch of body parts touching and untouching every second, thats may be the cause…

or you can also add a delay…

local Activated = false
script.Parent.Parent.Part.Touched:Connect(function(hit)
	local humanoid = hit.Parent:FindFirstChildOfClass("Humanoid")
	if humanoid then
		if not Activated then
			script.Parent.ClickDetector.MaxActivationDistance = 0
			Activated = true
		end
	end
end)

If your goal is for the click detector to deactivate while the character is touching a part, then reactivate when the character stops touching it, then this script should work to achieve that:

local instance = script.Parent
local clickDetector = instance.ClickDetector
local part = instance.Parent.Part

local active = {}

local function getHumanoid(otherPart)
	local model = otherPart:FindFirstAncestorOfClass("Model")

	if model then
		return model:FindFirstChildWhichIsA("Humanoid")
	end
end

local function onTouched(otherPart)
	local humanoid = getHumanoid(otherPart)

	if humanoid and not active[humanoid] then
		active[humanoid] = true

		clickDetector.MaxActivationDistance = 0
	end
end

local function onTouchEnded(otherPart)
	local humanoid = getHumanoid(otherPart)

	if humanoid and active[humanoid] then
		active[humanoid] = nil

		clickDetector.MaxActivationDistance = 200
	end
end

part.Touched:Connect(onTouched)
part.TouchEnded:Connect(onTouchEnded)

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