Script teleports to mouse despite being locked on

Hey, so I have a teleport script. If the user is not locked on, it’s supposed to teleport to the mouse and if they are locked on, it’s supposed to teleport to the target, but sadly, despite being locked on, it teleports to the mouse, any idea why?

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local ServerStorage = game:GetService("ServerStorage")

local Modules = ServerScriptService:WaitForChild("Modules")

local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local TeleportRemote = Remotes.RequestTeleport
local Utils = require(Modules:WaitForChild("Utils"))
local CharacterAbilities = require(Modules:WaitForChild("CharacterAbilities"))

TeleportRemote.OnServerEvent:Connect(function(plr, mouse)
	local Data = Utils.GetData(plr)
	if not Data then return plr:Kick(Utils.Error("No data.")) end
	local PlayerChar = Data.Character.Value
	if PlayerChar ~= "None" then
		local plrchar = plr.Character
		local Head = plrchar.Head
		local TPRange = CharacterAbilities[PlayerChar].TPRange
		local Magnitude = (Head.Position - mouse).Magnitude
		local HumanoidRootPart = plrchar.HumanoidRootPart
		if Data.lockedOnto.Value ~= "None" then
			local playerlocked = Data.lockedOnto.Value
			local Players = game:GetService("Players")
			local playertar = Players:FindFirstChild(playerlocked)
			local targetchar = playertar.Character
			local targethead = targetchar.Head
			local Magnitude2 = (Head.Position - targethead.Position).Magnitude
			if TPRange > Magnitude2 then
				local OFFSET = 4
					targetchar:WaitForChild("HumanoidRootPart").CFrame = plrchar:WaitForChild("HumanoidRootPart").CFrame * CFrame.new(0, 0, -OFFSET)
				end
			end
		if TPRange > Magnitude then
			HumanoidRootPart.Position = mouse
		else
			print("insufficient tprange")
		end
	end
	
end)```
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local ServerStorage = game:GetService("ServerStorage")

local Modules = ServerScriptService:WaitForChild("Modules")

local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local TeleportRemote = Remotes.RequestTeleport
local Utils = require(Modules:WaitForChild("Utils"))
local CharacterAbilities = require(Modules:WaitForChild("CharacterAbilities"))

TeleportRemote.OnServerEvent:Connect(function(plr, mouse)
	local Data = Utils.GetData(plr)
	if not Data then return plr:Kick(Utils.Error("No data.")) end
	local PlayerChar = Data.Character.Value
	if PlayerChar ~= "None" then
		local plrchar = plr.Character
		local Head = plrchar.Head
		local TPRange = CharacterAbilities[PlayerChar].TPRange
		local Magnitude = (Head.Position - mouse).Magnitude
		local HumanoidRootPart = plrchar.HumanoidRootPart
		if Data.lockedOnto.Value ~= "None" then
			local playerlocked = Data.lockedOnto.Value
			local Players = game:GetService("Players")
			local playertar = Players:FindFirstChild(playerlocked)
			local targetchar = playertar.Character
			local targethead = targetchar.Head
			local Magnitude2 = (Head.Position - targethead.Position).Magnitude
			if TPRange > Magnitude2 then
				local OFFSET = 4
				targetchar:WaitForChild("HumanoidRootPart").CFrame = plrchar:WaitForChild("HumanoidRootPart").CFrame * CFrame.new(0, 0, -OFFSET)
			end
		elseif TPRange > Magnitude then
			HumanoidRootPart.Position = mouse
		else
			print("insufficient tprange")
		end
	end
	
end)
1 Like