Roblox 传送功能
文件目录
新建一个Model,Model下新建一个Script文件和一个部件取名为teleportPart
ReplicatedStorage下新建一个ModuleScript取名为TeleportWithinPlace
Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TELEPORT_DESTINATION = Vector3.new(50, 0, 50) --传送的目的地
local TELEPORT_FACE_ANGLE = 0 --传送后角色朝向的角度
local FREEZE_CHARACTER = true --传送过程是否阻止移动
-- Require teleport module
local TeleportWithinPlace = require(ReplicatedStorage:WaitForChild("TeleportWithinPlace"))
local function teleportPlayer(otherPart)
local character = otherPart.Parent
local humanoid = character:FindFirstChild("Humanoid")
if humanoid and not humanoid:GetAttribute("Teleporting") then
humanoid:SetAttribute("Teleporting", true)
local params = {
destination = TELEPORT_DESTINATION,
faceAngle = TELEPORT_FACE_ANGLE,
freeze = FREEZE_CHARACTER
}
TeleportWithinPlace.Teleport(humanoid, params)
wait(1)
humanoid:SetAttribute("Teleporting", nil)
end
end
script.Parent.teleportPart.Touched:Connect(teleportPlayer)
TeleportWithinPlace
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local TeleportWithinPlace = {}
-- Create remote event instance
local teleportEvent = Instance.new("RemoteEvent")
teleportEvent.Name = "TeleportEvent"
teleportEvent.Parent = ReplicatedStorage
function TeleportWithinPlace.Teleport(humanoid, params)
local character = humanoid.Parent
-- Freeze character during teleport if requested
if params.freeze then
humanoid:SetAttribute("DefaultWalkSpeed", humanoid.WalkSpeed)
humanoid:SetAttribute("DefaultJumpPower", humanoid.JumpPower)
humanoid.WalkSpeed = 0
humanoid.JumpPower = 0
end
-- Calculate height of root part from character base
local rootPartY
if humanoid.RigType == Enum.HumanoidRigType.R15 then
rootPartY = (humanoid.RootPart.Size.Y * 0.5) + humanoid.HipHeight
else
rootPartY = (humanoid.RootPart.Size.Y * 0.5) + humanoid.Parent.LeftLeg.Size.Y + humanoid.HipHeight
end
-- Teleport player and request content around location if applicable
local position = CFrame.new(params.destination + Vector3.new(0, rootPartY, 0))
local orientation = CFrame.Angles(0, math.rad(params.faceAngle), 0)
if workspace.StreamingEnabled then
local player = Players:GetPlayerFromCharacter(character)
player:RequestStreamAroundAsync(params.destination)
end
character:SetPrimaryPartCFrame(position * orientation)
-- Unfreeze character
if params.freeze then
humanoid.WalkSpeed = humanoid:GetAttribute("DefaultWalkSpeed")
humanoid.JumpPower = humanoid:GetAttribute("DefaultJumpPower")
end
end
return TeleportWithinPlace
teleportPart
一个普通的部件。
传送方法:
传送方式为触碰部件,触发传送