Hey, so I have this teleport script but it teleports even if the player is farther than the allowed range, any idea why? Here is my code!
--Services
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local ServerStorage = game:GetService("ServerStorage")
--Folders
local Modules = ServerScriptService:WaitForChild("Modules")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")
--Remotes
local TeleportRemote = Remotes:WaitForChild("RequestTeleport")
--Modules
local Utils = require(Modules:WaitForChild("Utils"))
local CharacterAbilities = require(Modules:WaitForChild("CharacterAbilities"))
--On server event
TeleportRemote.OnServerEvent:Connect(function(plr, mouse)
--Get player data
local Data = Utils.GetData(plr)
if not Data then return plr:Kick(Utils.Error("No data.")) end
--Check player fighter
local PlayerFighter = Data.Character.Value
if PlayerFighter ~= "None" then
--If player has a character selected, we get that characters teleport range
local CharTPRange = CharacterAbilities[PlayerFighter].TPRange
--Next, we declare some variables regarding Player's character
local PlayerChar = plr.Character
local PlayerRoot = PlayerChar.HumanoidRootPart
local PlayerHead = PlayerChar.Head
--Now, let's check if the player is currently locked onto somebody.
local LockedValue = Data.lockedOnto.Value
--If the player is locked, let's teleport to the locked on player.
if LockedValue ~= "None" then
--Let's get some info about the locked on player.
local TargetPlayer = Players:FindFirstChild(LockedValue)
local TargetChar = TargetPlayer.Character
local TargetRoot = TargetChar.HumanoidRootPart
local TargetHead = TargetChar.Head
--Let's see if the location of the 2nd player does not exceed the maximum allowed Teleport Range.
local MagnitudeLock = (PlayerHead.Position - TargetHead.Position).Magnitude
if MagnitudeLock > CharTPRange then
--If everything is alright, we can proceed with the teleport. First, let's set the offset of the player.
local OFFSET = 4
--Before we completely teleport, let's check if the player has sufficient mana to teleport.
local Mana = Data.Mana.Value
local ManaCost = CharacterAbilities[PlayerFighter].TPManaCost
if Mana >= ManaCost then
--Let's deduct the mana.
local ManaEquation = Mana - ManaCost
local Mana = ManaEquation
--Now we teleport!
PlayerRoot.CFrame = TargetRoot.CFrame * CFrame.new(0, 0, -OFFSET)
end
end
end
end
end)