How check if a player is inside an Part?

Iam creating a game like Power Simulator, so i started create the training areas, i want check when player enter on area and when player leave, i tried used Touch and TouchEnded(the area is a Part), but every times touch and touchended active together, iam 2 days traying fix that, anyone can helpme ?

Code:

    --Variables
local Part = script.Parent
local module = require(game.Workspace:WaitForChild("ModuleScript"))
local debounce = false

script.Parent.Touched:Connect(function(hit) 
	if not module.Get then 
      if hit.Parent:FindFirstChild("Humanoid") and debounce== false then
      debounce = true
      print(hit.Parent.Name)
      local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
      print(player.Name)
      module.Training(player,50,Part,"Endurance",true)
      wait(1)
      debounce = false
      end
    end
end)

script.Parent.TouchEnded:Connect(function(hit) 
	if module.Get then 
      if hit.Parent:FindFirstChild("Humanoid") and debounce== false then
      debounce = true
      print(hit.Parent.Name.."Leaved")
      local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
      print(player.Name)
      module.Training(player,50,Part,"Endurance",true)
      wait(1)
      debounce = false
      end
    end
end)

The part is here

“ignore the green one, and CanCollide is false, so player can enter”

This is my module

    local touching = false
local StartTraining = game.Workspace:WaitForChild("StartTraining")
local StopTraining = game.Workspace:WaitForChild("StopTraining")

local module = {}
    function module.Get()
	   return touching
    end
    
    function module.Training(player,amount,Part,Stat,Touchings)
	   touching = Touchings
	   if touching then
		  StartTraining:Fire(player,amount,Part,Stat)
	   else 
	     
	   end
    end
1 Like

hi, u can use Region3, use this tutotial: https://www.youtube.com/watch?v=kRuSWSe9faY

Region3 and some use of RunService u can do that easy

6 Likes

Safe make a infinity while ? cant cause lag?

1 Like

u can use wait(0.1) to don’t have lag and that is just a tutorial,don’t a advanced script but if u use RunService it will don’t have lag

why ? how can i use RunService to fix that ?

RunSevice and Region3, Region3 to detect if the players is in a region and RunService is like wait(0.02) but without lag. Edit: At RunService u need to use Heartbeat

i didnt understand how use RunService on this case, can you send a small code, iam really not understanding

a code using print:

local RS = game:GetService('RunService')
RS.Heartbeat:Connect(function()
      print('example')
end)


Thanks, also exploits cant see Script Inside Part, can they ?

no, only they can detect the script in it, but they can if ur script have a RemoteEvent or RemoteFunction in it

Region3 and .Touched won’t detect if a character is fully inside a part. I would suggest using something like Magnitude and messing around with the range to get exactly when a character is fully inside of a part. Also exploiters can see read scripts on the client(local scripts) via bytecode, however, they cannot read scripts on the server.

1 Like

I never used that, have any tutorial ?

I would suggest looking into what magnitude really is. For a simple explanation, magnitude is the length of a vector.

u can use unpack and some function to detect if the player is r6 or r15, the unpack will get a table with all the parts in Region if it have all parts in the Character then it will be true, and its more good to use Region3 with a WhiteList that is the Character, magnitude is realy hard to use and get the good numbers

That’s another way you can do however, like I said, it won’t fully detect if every bodypart is within it. A body part could be touching the Region3 and it would detect it, but the whole body part might not be fully inside the Region3. With some simple math, you can get the distance between the center of a Region3 to an edge. Then you can set the magnitude range to that distance.

Dont need, is a power Simulator, even if he is not fully inside he will start receiving status, and if he is not strong enough to be there he will take damage anyway, there is no reason for him to be fully inside

1 Like

Then I suggest, you use Region3.

yeah, but magnitude isn’t better becouse it only search a circle area, but if he want to make two areas, one circle like in photo and one triangle or cubic? it will break so Region3 is better than magnitude

Another question, you said “no, only they can detect the script in it, but they can if ur script have a RemoteEvent or RemoteFunction in it”, iam using BlindEvent, because the stats arent on folder, so i need use ServeScriptService to make the Training Script

Magnitude isn’t for searching. Like I said, it’s the length of a vector. You can always get the magnitude from the center of the part(part.CFrame) to the edge of a part(part.Size.X / 2). You can use that magnitude as a range.