Teleport Script

Hey guys, I don’t have a good grasp of raycasting and wondered how to make it so this teleport script doesn’t make you go through walls. and maybe make it instead teleport a random direction if there is a wall in front of you, say, left or right?

local function TeleportUnlessWall(char: Model)
	local Direction = char:GetPivot().LookVector
	local Params = RaycastParams.new()
	Params.FilterDescendantsInstances = {char}
	Params.FilterType = Enum.RaycastFilterType.Blacklist
	Params.RespectCanCollide = true
	local origin = char:GetPivot().Position
	local Results = workspace:Raycast(origin,Direction,Params)
	if Results then
		char:PivotTo(CFrame.new(Results.Position - (Direction.Unit*2)))
	else
		char:PivotTo(char:GetPivot() + Direction * 15)
	end
end

Unsure what the question is here? This looks like it already does that.

It doesn’t, the code teleports you where you’re looking and if there is a wall in front of you it teleport you through the wall. I don’t understand how I would make it so it doesn’t teleport you through a wall, and also instead of forward choose a random direction.

When you don’t get a non-nil Results, you are always going ahead 15 studs, but I think the cast is too short. Iirc the length of Direction matters, try doing:

local Results = workspace:Raycast(origin, Direction * 15, Params)

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