Roblox 传送功能

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

一个普通的部件。

传送方法:

传送方式为触碰部件,触发传送

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值