Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make a value true while a player is touching it?

I am trying to make it so that a value is true whenever you are touching the scripts parent, however i'm having a lot of trouble. The Touch Ended is not reliable enough as it constantly goes off even when the player is still on the part, it is too inconsistent, and since the Touched function usually activates only once, the value stays at false even though you are still touching it. I have put my full script and a simplified version of the script I'm using below. Anyone know what I should do?

FULL SCRIPT

part=script.Parent
ColorActive="Forest green"
ColorInActive="Institutional white"
part.Touched:connect(function(hit)--This only activates once usually
	if hit.Parent:FindFirstChild("Humanoid")~=nil then
		touch = true
		local Touching=script.Parent.Touching
		part.BrickColor=BrickColor.new(ColorActive)
		repeat 
			--print"Touching"
			Touching.Value=true
			wait(1)
		until not touch
		part.BrickColor=BrickColor.new(ColorInActive)
		Touching.Value=false
	end
end)
part.TouchEnded:connect(function() touch = false end)--If this activates while a person is still on it, then it won't activate again until they get off and get back on.

SIMPLE

part.Touched:connect(function(hit)--This only activates once usually
touch = true
repeat 
wait(1)
script.Parent.Touching.Value=true
until not touch
script.Parent.Touching.Value=false
end)
part.TouchEnded:connect(function() touch = false end)----If this activates while a person is still on it, it won't activate again until they get off and get back on, which isn't supposed to happen

1 answer

✓ Accepted answer

The best way to go about doing this is using Magnitude, this allows you to get the distance between 2 parts. You could use this to check if the player is within a certain distance of the script's parent. You could use a while wait() do loop to constantly scan, a for loop to go through each of the players and getting their .Character, then finding the Magnitude between the player's torso and the part. Since this is a slight request, I'm telling you what scripting knowledge you need to do this. If this helped please vote up and accept as the answer!

This discussion is preserved as it was. New answers, comments, and votes are closed.