我一直在尝试帮助我的朋友制作随机标题屏幕启动画面(就像在《我的世界》或《泰拉瑞亚》中一样),但我在实际更改文本方面遇到了一些问题。我尝试了几种不同的方法,但到目前为止没有一个有效,这些是:
local text = game.StarterGui.MainMenu.splash
local splashText = text.Text
local splash = math.random(1,2)
local event = game.ReplicatedStorage.splash
print(splash)
if splash == 1 then
splashText = "a"
print("Complete")
elseif splash == 2 then
splashText = "b"
print("Complete")
end
脚本1(本地脚本):
local splash = math.random(2,3)
local event = game.ReplicatedStorage.splash
print(splash)
event:FireServer(splash)
脚本2(普通脚本):
local event = game.ReplicatedStorage.splash
event.OnServerEvent:Connect(function(plr, splash)
print(splash)
local text = plr.PlayerGui.MainMenu.splash
local splashText = text.Text
if splash == 1 then
splashText = "a"
print("Complete")
elseif splash == 2 then
splashText = "b"
print("Complete")
end
end)
local text = game.Players.LocalPlayer.PlayerGui.MainMenu.splash
local splashText = text.Text
local splash = math.random(1,2)
local event = game.ReplicatedStorage.splash
print(splash)
if splash == 1 then
splashText = "a"
print("Complete")
elseif splash == 2 then
splashText = "b"
print("Complete")
end
local text = game.StarterGui.MainMenu.splash
local splash = math.random(1,2)
local event = game.ReplicatedStorage.splash
print(splash)
if splash == 1 then
text.Text = "a"
print("Complete")
elseif splash == 2 then
text.Text = "b"
print("Complete")
end
有人知道有什么方法可以实现这项工作吗?
game.StarterGui
中的所有内容都会克隆到PlayerGui。现在您正在更改 game.StarterGui.TextLabel
,但不更改 game.Players.<playername>.PlayerGui.ScreenGui.<...>
中的那个。
文件结构如下
StarterGui
| ScreenGui
| | Frame
| | | TextLabel
| | | | LocalScript
我使用了框架,但结构如你所愿。
LocalScript
local textLabel = script.Parent
local splashTexts = {"foo", "bar"}
textLabel.Text = splashTexts[math.random(1, #splashTexts)]
-- uses the length syntax, #, for length of table
每个玩家都会有不同的启动文本,并且每次重生时都会改变(因为每次重生时都会重新制作 PlayerGui)。如果这是一个问题,您可以将其存储为 LocalPlayer
上的属性。
local textLabel = script.Parent
local splashTexts = {"foo", "bar"}
local localPlayer = game:GetService("Players").LocalPlayer
local chosenText = localPlayer:GetAttribute("SplashText")
if (chosenText == nil) then
chosenText = splashTexts[math.random(1, #splashTexts)]
localPlayer:SetAttribute("SplashText", chosenText)
end
textLabel.Text = chosenText