Run Script While Touching Part

I’m attempting to make a ‘heal pad’ where it heals you while you’re on top of it, similar to [Overwatch] Soldier: 76’s Biotic Field, but I can’t seem to detect when the player isn’t touching the part. I’ve tried things like putting a BoolValue inside the character and then deleting it when they leave, and it works at first, but then it just starts spam creating/deleting it and eventually gives up. This was done with the TouchEnded event. I don’t know if this is even possible, but I’m sure someone out there knows how.

2 Likes

You should try using Raycasting for this :smiley:

local HealthPads = workspace.Healthpads;
local root_PART = path.HumanoidRootPart;
local RaycastParams = RaycastParams.new();
RaycastParams.FilterDescendantsInstances = HealthPads:GetChildren();
RaycastParams.FilterType = Enum.RaycastFilterType.Whitelist;

game:GetService("RunService").RenderStepped:Connect(function()
   local castResults = workspace:Raycast(root_PART.CFrame.p, vector3.new(0,-20,0), RaycastParams);
      if(castResults)then
           --<Is "Touching" Part;
else
            --<Not "Touching" Part
      end;

end);

I’ve never actually used raycast params, but i’m pretty sure castResults will be nill if it doesn’t hit, if not, you can use castResults.Instance

Maybe you could try using the :GetTouchingPart in your script and then just doing an if and else function.

1 Like

Could you tell me a bit about the ray? I’m guessing it sends a box striaght up to see if there’s a player?

local Field = script.Parent
local FieldBeingTouched = false

Field.Touched:Connect(function(hit)
	local Humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
	if Humanoid then
		FieldBeingTouched = true
		while FieldBeingTouched == true do
			Humanoid.Health = Humanoid.Health + 5
			wait(0.2)
		end
	end
end)

Field.TouchEnded:Connect(function(hit)
	local Humanoid = hit.Parent:FindFirstChildWhichIsA("Humanoid")
	if Humanoid then
		FieldBeingTouched = false
	end
end)


1 Like

Are you sure that works? Thats pretty much exactly what I did.

Can you show us the script? .Touched events should work unless something’s wrong.

I deleted the script when I was messing with it, but it was something like

hitObject.TouchEnded:Wait()
heal = false

Yes, I tested it out, it works perfectly fine.
Have you tried it?

No, actually, I just don’t want to use more time than I need to, this is one of my biggest projects and I want to finish it. Not that I don’t have time for testing your ideas, but I just want to be able to make a lot of progress.

So you didn’t try my code that works?

I am now. I’m surprised it functions when mine didn’t. There are a couple differences but for the most part it seems the same to me.

1 Like

So if it worked, give a solution, so people know that’s its solved.

Maybe try Region3 or magnitude?