Player getting teleported even though player is outside region

I’m making a robbery system and everything is working normally, except when you get caught in the store after the timer runs out.

When the player is inside the store after the timer runs out the player gets teleported outside of the store but it will occasionally teleport you outside the store even if you’re not inside the store. Obviously this could be a problem if you’re across the map and you just randomly get teleported to a Jewelry store.

Code which teleports player:

RUN_SERVICE.Heartbeat:Connect(function()
	local parts = game.Workspace:GetPartBoundsInBox(area.CFrame, area.Size)
	
	for i, v in pairs(parts) do
		if v.Parent:FindFirstChild("Humanoid") then
			if SS.Robbery.Value == false then
				v.Parent.HumanoidRootPart.Position = game.Workspace:FindFirstChild("rob").Position
				break
			end
		end
	end
end)
Full Code
--//Constants
local ROBBERY_TIME = 5
local COOLDOWN = 10
local JEWEL_PARENT = game.Workspace
local SS = game:GetService("ServerStorage")
local RS = game:GetService("ReplicatedStorage")
local RUN_SERVICE = game:GetService("RunService")

--//Variables
local prompt = script.Parent
local glass = game.Workspace:FindFirstChild("glass")
local area = game.Workspace:FindFirstChild("area")

--//Events
prompt.Triggered:Connect(function(plr)
	glass.Transparency = 1
	glass.CanCollide = false
	prompt.Enabled = false
	SS.Robbery.Value = true
	plr.PlayerGui.RobUI.Enabled = true

	
	task.spawn(function()
		wait(ROBBERY_TIME)
		glass.Transparency = .4
		glass.CanCollide = true
		SS.Robbery.Value = false
		plr.PlayerGui.RobUI.Enabled = false
		
		for i, v in pairs(JEWEL_PARENT:GetChildren()) do
			if v.Name == "Jewel" then
				v:FindFirstChild("ProximityPrompt").Enabled = false
			end
		end
		
		wait(COOLDOWN)
		prompt.Enabled = true
	end)
	
	for i, v in pairs(JEWEL_PARENT:GetChildren()) do
		if v.Name == "Jewel" then
			v:FindFirstChild("ProximityPrompt").Enabled = true
			v.CanCollide = true
			v.Transparency = 0
		end
	end
	
	task.spawn(function()
		for i = ROBBERY_TIME, 0, -1 do
			plr.PlayerGui.RobUI.Frame.TextLabel.Text = "You have "..i.." seconds to rob"
			wait(1)
		end
	end)
end)

RUN_SERVICE.Heartbeat:Connect(function()
	local parts = game.Workspace:GetPartBoundsInBox(area.CFrame, area.Size)
	
	for i, v in pairs(parts) do
		if v.Parent:FindFirstChild("Humanoid") then
			if SS.Robbery.Value == false then
				v.Parent.HumanoidRootPart.Position = game.Workspace:FindFirstChild("rob").Position
				break
			end
		end
	end
end)

I’m a little new to the method :GetPartBoundsInBox so if anyone could help that’d be great!

Thanks,
dza
EDIT: Forgot to show video lol

When you use :GetPartBoundsInBox() it gets everything that’s inside of that part, even partially, which can slow down your game. I recommend using OverlapParams to filter out specific objects.

It might be that the player has one of their limbs inside of the area object.

I’ve tested it with my character’s parts and yeah it does do that but in the video (which i just added) you can see that it’s not even close to the area of the store

Maybe the teleportation should happen in the client. That’s my best guess so far. I have no idea what is causing the issue.

Alright, I’ll try that out although won’t it just think that the player’s character is still in the area on the server causing it to always teleport? Idk I’m just spit balling now

I finally fixed the issue, it was because i was setting the humanoidrootpart’s position and not the cframe :smiley:
Code that actually works:

RUN_SERVICE.Heartbeat:Connect(function()
	local parts = game.Workspace:GetPartBoundsInBox(area.CFrame, area.Size)
	
	for i, v in pairs(parts) do
		if v.Parent:FindFirstChild("Humanoid") then
			if SS.Robbery.Value == false then
				v.Parent:SetPrimaryPartCFrame(game.Workspace.rob.CFrame)
				break
			end
		end
	end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.