If Player is in an area

A Raycast is a line, while he is trying to make a sort of area. This would require a lot of Raycast.

1 Like

you can use raycast with magnitude ya know? additionally you could use region3 to make an area. Like theres so many better ways to do this.

1 Like

Can i have a code example so i can implement this

True, but if this is what he prefers or wants, I would gladly try to help him. He may change it in the future, but my primary goal here is to help him fix his errors.

1 Like

The code works and knows that I touched the part and it also printed that it worked but it did not make the button gui non visible. This my current code:

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 runservice = game:GetService("RunService")
function CheckIfPlayerIsInArea()
    local touching = Part:GetTouchingParts()
	for i=1,#touching do
		if touching[i] == game.Players.LocalPlayer.Character.HumanoidRootPart then
			game.Workspace.CheckParts.count.Value = true
			if game.Workspace.CheckParts.count.Value == true then
					CurrentStage.Visible = false
			end

			return true
			
		end
	end
	return false
end	
runservice.Stepped:Wait()
wait(5)
while true do 
	wait(2)
	Part.Touched:Connect(function() end)
CheckIfPlayerIsInArea()
end

Is this a local script or a server script?

1 Like

local script 30

Is the ScreenGUI enabled? Is the “CurrentStage” BackgroundTransparency 0?

1 Like

the screen gui is enabled and the transparency is 0

It’s true, not false. :flushed:

1 Like

no, i want it to be so that if they are near their checkpoint, they can not use the button.

Use Region3 it’s very useful in this case.

1 Like

how can i use it, a code example will be helpful

There is alot of tutorials on “Region3” watch one of them.

1 Like

This is my code now. Even though i put it to print player found if the part the player is inside has the same name as the players checkpoint, it still printed it and the button stayed visible. I was on checkpoint 1 but when i went to the part for checkpoint 0 it still did it.

local check = workspace.CheckParts
local player = game.Players.LocalPlayer
local stage = player.leaderstats.Stage

local CurrentStage = game.StarterGui.MainGui.Teleporters.Frame.CurrentStage
local runservice = game:GetService("RunService")
local pos1 = check[stage.Value].Position - (check[stage.Value].Size/2)
local pos2 = check[stage.Value].Position + (check[stage.Value].Size/2)
local region = Region3.new(pos1,pos2)
while true do
	wait()
	local partsinregion = workspace:FindPartsInRegion3(region, nil, 1000)
	for i , part in pairs(partsinregion) do
		if part.Parent:FindFirstChild("Humanoid") ~= nil then
			print('Player found in region!:'.. part.Parent.Name)
			CurrentStage.Visible = false
		end
	end
end

Can you give me a code example for help?

The code example will be more helpful if it was from my script above.no one is replying, lol help

Helpful to state them then point that out.


@TheDragonSlayer5481 here’s examples of how you could implement this functionality

Magnitude check

local function CheckIfPlayerIsInArea(part, character, studs)
    local t1, t2 = typeof(part), typeof(character)
    assert(t1 == "Instance" and t2 == "Instance", ("part, character expected; got %s, %s"):format(t1, t2))
    local pos = character:GetModelCFrame().p
    local dist =  (pos - part.Position).Magnitude
    return dist <= (studs or 100), dist -- 100 default
end

 print(CheckIfPlayerIsInArea(workspace.Part, workspace.XxELECTROFUSIONxX, 50))

Region3

local function CheckIfPlayerIsInArea(part, character, studs)
	local region = Region3.new(part.Position - Vector3.new(studs, studs, studs), part.Position + Vector3.new(studs, studs, studs)) 
	return table.find(workspace:FindPartsInRegion3(region, part, game.Players.MaxPlayers), character.HumanoidRootPart) ~= nil       
end     

print(CheckIfPlayerIsInArea(workspace.Part, workspace.XxELECTROFUSIONxX, 20))
1 Like

You could try using Region3WithWhitelist.

Something like this:

function getPlayersInPart(RegionPart)
	-- Setup region3 based on BasePart
	local min = RegionPart.Position - (0.5 * RegionPart.Size)
	local max = RegionPart.Position + (0.5 * RegionPart.Size)
	local region = Region3.new(min, max)
	-- add every player's HumanoidRootPart to a whitelist
	local whitelist = {}
	for i,v in ipairs(game.Players:GetPlayers()) do
		if v.Character and v.Character.HumanoidRootPart then
			table.insert(whitelist,v.Character.HumanoidRootPart)
		end
	end
	-- Find parts inside Region3 using list of HumanoidRootParts as whitelist
	local PlayersInRegion = {}
	local partsInRegion = workspace:FindPartsInRegion3WithWhiteList(region,whitelist, 100)
	for partIndex, part in pairs(partsInRegion) do
		table.insert(PlayersInRegion,game.Players:GetPlayerFromCharacter(part.Parent))
	end
	return PlayersInRegion
end
1 Like