If Player is in an area

I am trying to make a function to check if the player is touching a part. The part is chosen through a variable which looks at a players checkpoint value. So if I am at checkpoint 1, it will look for the part with the number 1 as its name within a certain folder. I am trying to make it so if they are at the checkpoint, they can not use a button which teleports them back to their current stage for fairness issues. I know in my script (below) I use GetTouchingParts but I tried putting my part CanCollide to true but it still did not work so I have a touched event for a touch interest. Overall, it is not working. I got this error.

The script:

local check = workspace.CheckParts
local player = game.Players.LocalPlayer
local stage = player.leaderstats.Stage
local Part  =check[stage.Value]
local CurrentStage = game.StarterGui.MainGui.Teleporters.Frame["Current Stage"]
Part.Touched:Connect(function() end)
function CheckIfPlayerIsInArea(Part,Character)
    local touching = Part:GetTouchingParts()
	for i=1,#touching do
		if touching[i] == Character.HumanoidRootPart then
			print("worked")
	CurrentStage.Visible = false
			return true
			
		end
	end
	return false
end
CheckIfPlayerIsInArea()
1 Like

well not 2 touched fucntions >you have 1 function for touched and then another function for no reason

1 Like

This is for the touch interest. For get touching parts you need a touch interest if the part is not cancollide.

here: Simple trick to make GetTouchingParts work with non-CanCollide parts

1 Like

That doesn’t answer my question, you still only need 1 function

but you need the touch interest for the function to work

yeah the touched is a function itself

ik but it make a touch interest which you need for the other function

script.Parent.Touched:Connect(function()

end) <what do you think this is?

ik check this

you dont need another function

1 Like

i think you do 30 charssssss

just go to the wiki, like you’re pulling all this information you don’t understand

1 Like

:GetTouchingParts() does not work on characters as the legs and arms are non-colide, therefore if you want to have to do methods like so which @buildthomas showed previously:

Alternatively, you might want to look into regions instead as they can provide the same relative ideology and encompass the same logic.

1 Like

I’ll explain. The reason why @TheDragonSlayer5481 added a Touched function is to also register Non-Collideable parts. GetTouchingParts will only return parts who’s property CanCollide is true. However, by adding a touch interest, it will register the part despite not having a CanCollide true.

Also, I see you have 2 variables that has the same name as “Part” (In a local variable and parameter).

And when you did CheckIfPlayerIsInArea(), you didn’t add a part argument.

1 Like

You may want to use .magnitude:

local check = workspace.CheckParts
local player = game.Players.LocalPlayer
local stage = player.leaderstats.Stage
local Part  =check[stage.Value]
local CurrentStage = game.StarterGui.MainGui.Teleporters.Frame["Current Stage"]

local magnitude = (player.Character.HumanoidRootPart.Position - Part.Position).Magnitude
if magnitude == 0 then
CurrentStage.Visible = false
else
CurrentStage.Visible = true
end

Magnitude detects the distance between the players position and the parts position. It helps in these situations.
Hope it helped

1 Like

this is because the part , check[stage.Value], when i try to put it in the parameters it does not work

Then maybe you should remove the “Part” from function CheckIfPlayerIsInArea(Part,Character) .

Another note, be sure to disconnect the Touched function after you did GetTouchingParts.

1 Like

This helps, but i do not want the player to have the exact position but within a certain area.

but that doesn’t make any sense to me > why not just use raycasting at that point

1 Like

Could you help me and show me a script example on how to do that?