I've been trying to help my friend making a randomized title screen splash (like in Minecraft or Terraria) but I've been having some problems with having the text actually change. I've tried a few different methods but none of them have worked so far, these are:
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
Script 1 (Local Script):
local splash = math.random(2,3)
local event = game.ReplicatedStorage.splash
print(splash)
event:FireServer(splash)
Script 2 (Normal Script):
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
Is there any way anybody knows of making this work?
You have to consider that everything in game.StarterGui
is cloned to the PlayerGui. Right now you are changing the game.StarterGui.TextLabel
but not the one in game.Players.<playername>.PlayerGui.ScreenGui.<...>
.
The file structure is as follows
StarterGui
| ScreenGui
| | Frame
| | | TextLabel
| | | | LocalScript
I've used a Frame, but structure as you wish.
LocalScript
local textLabel = script.Parent
local splashTexts = {"foo", "bar"}
textLabel.Text = splashTexts[math.random(1, #splashTexts)]
-- uses the length syntax, #, for length of table
Every player will have a different splash text and this will also change every time they respawn (as the PlayerGui is remade every-time they respawn). If this is a problem you can store it as an attribute on the 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