Better Teleport Script?

Below, I have created a teleportation script where when the function is triggered, teleports you within a range of 25 studs (this teleport system makes it so you don’t teleport into walls). But for some reason, it messes with the Humanoid Root Part (I have other scripts that play an effect on the humanoid root part but is offset, probably from this code). Also, this code is really inconsistent, is there a better way to do this or any edits needed to be made?

enabled = true
cd = false
local cooldown =  10
local remote =  game.ReplicatedStorage.CombatRemote.subs

function TeleportUnlessWall(Object : BasePart,Direction : Vector3)
	local Params = RaycastParams.new()
	Params.FilterDescendantsInstances = {Object}
	Params.FilterType = Enum.RaycastFilterType.Blacklist
	Params.RespectCanCollide = true
	local Results = workspace:Raycast(Object.Position,Direction,Params)
	if Results then
		Object.Position = Results.Position - (Direction.Unit*2)
	else
		Object.Position += Direction
	end
end

remote.OnServerEvent:Connect(function(plr)
	local player = plr
	if cd == false and player.Chakra.Value >= 10 then

	player.Chakra.Value -= 10
	local Character = player.Character
	local blood = game.ReplicatedStorage.Blood:Clone()
	local log = game.ReplicatedStorage.Log:Clone()
	blood.Position = Character.Torso.Position
	blood.Parent  = game.Workspace
	log.Parent = game.Workspace
	log.Position = Character.Torso.Position
	log.Sound:Play()
	cd = true
	TeleportUnlessWall(player.Character.HumanoidRootPart,Vector3.xAxis*25)
		game.Debris:AddItem(blood, 1)
		game.Debris:AddItem(log, 5)
wait(10)
	cd = false
end

end)

3 Likes

Instead if changing hrp’s position, use Character:PivotTo(CFrame)

Also change object class to model

i’m having some trouble with this, so far i have done this, it does not work and there are no errors

enabled = true
cd = false
local cooldown = 10
local remote = game.ReplicatedStorage.CombatRemote.subs

function TeleportUnlessWall(Object : BasePart,Direction : Vector3)
local Params = RaycastParams.new()
Params.FilterDescendantsInstances = {Object}
Params.FilterType = Enum.RaycastFilterType.Blacklist
Params.RespectCanCollide = true
local Results = workspace:Raycast(Object.CFrame,Direction,Params)
if Results then
Object:PivotTo(Results.CFrame - (Direction.Unit*2))
else
Object.CFrame += Direction
end
end

remote.OnServerEvent:Connect(function(plr)
local player = plr
if cd == false and player.Chakra.Value >= 10 then
cd = true
player.Chakra.Value -= 10
local Character = player.Character
local blood = game.ReplicatedStorage.Blood:Clone()
local log = game.ReplicatedStorage.Log:Clone()
blood.Position = Character.Torso.Position
blood.Parent = game.Workspace
log.Parent = game.Workspace
log.Position = Character.Torso.Position
log.Sound:Play()
TeleportUnlessWall(player.Character.PrimaryPart,Vector3.xAxis*25)
game.Debris:AddItem(blood, 1)
game.Debris:AddItem(log, 5)
wait(10)
cd = false
end

end)

You need to Pivot the model, not the primarypart

you cant pivot models using this combined with cframe or vector3, you need to move it through primary part

I’ve had issues with directly setting the HRP’s cframe. I suggest trying :PivotTo() instead like @Maytidis_good said.

Something like this should work.

local enabled = true
local db = false
local cooldown = 10
local remote = game.ReplicatedStorage.CombatRemote.subs

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)
	end
end

remote.OnServerEvent:Connect(function(player)
	if not db and player.Chakra.Value >= 10 then
        db = true
        task.delay(cooldown, function()
            db = false
        end)
        player.Chakra.Value -= 10
        local Character = player.Character
        local blood = game.ReplicatedStorage.Blood:Clone()
        local log = game.ReplicatedStorage.Log:Clone()
        blood.Position = Character.Torso.Position
        blood.Parent  = game.Workspace
        log.Parent = game.Workspace
        log.Position = Character.Torso.Position
        log.Sound:Play()
        TeleportUnlessWall(player.Character)
        game.Debris:AddItem(blood, 1)
        game.Debris:AddItem(log, 5)
    end
end)
4 Likes

i am confused on how this system works, like how would i increase the maximum distance?

Multiply the Direction by the distance you want, I forgot to do that in my script.

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