简体   繁体   中英

Why doesn't this change gui code work for Roblox Studio?

I am trying to create a TextLabel that changes every 5 seconds. I have this code, but it doesn't work.

local player = game.Players:GetPlayerFromCharacter(part.Parent) —this is our gateway to getting the PlayerGui object.
  local PlayerUI = player:WaitForChild(“PlayerGui”)
  local txtLabel = PlayerUI[“Welcome_Text”].TextLabel
while x < 1 do
  wait(5)
  txtLabel.Text = “Welcome to The Shadow Realm!”
  wait(5)
  txtLabel.Text = “Warning: This game contains scenes that may be too scary for some roblox players”
end 

I am getting an error message that says.

ServerScriptService.Script:2: attempt to index global 'part' (a nil value)

I don't know where to put my gui.

If I am correctly understanding what you are trying to do, you should be able to create a ScreenGui and place it in StartGui so that every player will have it copied to his PlayerGui when he joins the game. You could place a LocalScript inside of that GUI that would control the text on screen.

I see your LocalScript looking something like this:

-- Customize names on your own; these are just generic placeholders.
-- The structure of the GUI would look like this:
--
-- ScreenGui
--    LocalScript
--    Frame
--        TextLabel

local WELCOME = "Welcome to the Shadow Realm!"
local WARNING = "Warning! This game contains scenes that may be too scary for some players"

local runService = game:GetService("RunService")

local gui = script.Parent
local frame = gui.Frame
local label = frame.TextLabel

local function update_text()
    label.Text = WELCOME
    wait(5)
    label.Text = WARNING
    return
end
runService.RenderStepped:Connect(update_text)

Making this a LocalScript within a client-side GUI shifts the overhead to the client and eliminates the need to use the method Players::GetPlayerFromCharacter .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM