传送玩家
Coddy Roblox 游戏:灾难生存 课程的第 6 课,共 20 课。
没有人站在上面的岛屿就不是游戏。每轮开始时,所有玩家都会前往地图的 Spawn,结束时则回到大厅。
Players:GetPlayers() 会列出游戏中的所有玩家,而玩家的角色是一个 Model,即 player.Character。Model:PivotTo(cframe) 可以一次移动整个模型,同时让每个部件保持原来的位置。CFrame 是位置加上旋转,给它加上 Vector3 就能移动它:
local function teleport(player, target)
local character = player.Character
if character then
character:PivotTo(target.CFrame + Vector3.new(0, 4, 0))
end
end向上移动 4 studs,可以让玩家出现在目标上方一点,而不是目标内部。玩家加入游戏或死亡后,player.Character 会短暂地变成 nil,所以这里需要用 if。
挑战
中等把玩家传送到岛上,再传送回来。
- 编写
teleport(player, target):如果player.Character存在,就调用PivotTo(target.CFrame + Vector3.new(0, 4, 0))。 - 地图加载完成后,立即把每位玩家传送到
map.Spawn。 - 回合倒计时结束时,在地图被销毁之前,把所有人传送到
lobbySpawn。
按下“开始”:回合开始时,你会抵达岛上;回合结束时,你会回到大厅。然后按下“检查”。
亲手试试
local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")
local Settings = require(game.ReplicatedStorage.Settings)
print("Rounds last " .. Settings.RoundLength .. " seconds")
-- 每位玩家的排行榜上都有 Wins
Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local wins = Instance.new("IntValue")
wins.Name = "Wins"
wins.Parent = leaderstats
end)
-- 大厅里的状态板
local gui = Instance.new("SurfaceGui")
gui.Parent = workspace.StatusBoard
local status = Instance.new("TextLabel")
status.Name = "Status"
status.Size = UDim2.fromScale(1, 1)
status.BackgroundColor3 = Color3.fromRGB(25, 25, 35)
status.TextColor3 = Color3.fromRGB(255, 255, 255)
status.TextScaled = true
status.Text = "Welcome to Disaster Survival!"
status.Parent = gui
local function setStatus(text)
status.Text = text
end
-- 第 6 课:在这里编写传送玩家的代码
-- 回合循环
while true do
for seconds = Settings.Intermission, 1, -1 do
setStatus("Intermission: " .. seconds)
task.wait(1)
end
local map = ServerStorage.Maps.Island:Clone()
map.Name = "Map"
map.Parent = workspace
for seconds = Settings.RoundLength, 1, -1 do
setStatus("Time left: " .. seconds)
task.wait(1)
end
map:Destroy()
end
Roblox 游戏:灾难生存中的所有课
自己练习: Roblox 在线编辑器