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.
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()
: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.
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.
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