Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Trying to change ScreenGui Text?

Asked by 7 years ago

I am trying to change the text of a textbox inside a screen gui, when stepped upon a pressure plate.

Here is my code:

script.Parent.Touched:connect(function(hit)

local character = hit.Parent
local humanoid = character:FindFirstChild("Humanoid")

if humanoid and game.Players:GetPlayerFromCharacter(character) then

    local text = game.StarterGui.ScreenGui.TextBox.Text
    text = ("Welcome " .. character.Name .. "!")

end

end)

I am getting no errors, and have tried close to everything. Help!

3 answers

Log in to vote
0
Answered by
DanzLua 2879 Moderation Voter Community Moderator
7 years ago

The main problem in your script is that you're changing the textbox that is located inside of StarterGui. Children of this object get cloned into a Player's PlayerGui when their character spawns. So you won't be changing the gui of the player who touched the part. To fix this we get the player from the part that touched the part that the event is connected to.

local part = script.Parent

part.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(hit.Parent) then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        player.PlayerGui.ScreenGui.TextBox.Text="Welcome to this game ".. player.Name .. " !"
    end
end)

when the player touches the part, we check if first its parent has a humanoid, and then if there is a player associated with that model, if so then we can change the proper textbox.

It's important to remember that if this script is inside a server script it will not work with FilteringEnabled turned on. For that you'll have to use RemoteEvents

Ad
Log in to vote
0
Answered by
Asceylos 562 Moderation Voter
7 years ago
Edited 7 years ago

You need to change the GUI of the player, not the StarterGui.

local character = hit.Parent
local humanoid = character:FindFirstChild("Humanoid")

if humanoid and game.Players:GetPlayerFromCharacter(character) then

    local text = game.Players:GetPlayerFromCharacter(character).PlayerGui.ScreenGui.TextBox.Text
    text = ("Welcome " .. character.Name .. "!")

end
end)
0
also, I don't think you can use a property as a variable creeperhunter76 554 — 7y
0
You can. Asceylos 562 — 7y
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

For the part variable I just did script.Parent because I thought that the script was inside the part but if it isn't do workspace.YourPartName

local part = script.Parent
local text = game:GetService("StarterGui"):WaitForChild("ScreenGui"):WaitForChild("TextBox")
 -- Another way to type game.StarterGui.ScreenGui.TextBox
part.Touched;Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    local character = hit.Parent
    local txt = "Welcome to this game ".. character .. " !"
    if hit and hit.Parent and player then
        for i = 1,#txt do
            text.Text = string.sub(txt , 1 , i)  -- Text Popping up
            wait()
        end
        wait(1)
        for i = -1,#txt do
            text.Text = string.sub(txt , -1, i) -- Text deleting
            wait()
        end
        text.Visible = false
    end
end)

I made this script more complex but... You could also use the Clone() yeilding function Still Hopefully you can submit this answer This answer was complicated because I wanted to help you find any improvements and fixing the original original script (I added animating text btw if you didn't know) Hopefully you can accept my answer and this answer will work

0
The question was how to make it work, not how to make it better. Asceylos 562 — 7y
0
Yea it should work and make it better saSlol2436 716 — 7y
0
Thanks, I was actually trying to post a question but you answer it with string.sub. Thank you to both of you. @saSlol I'm testing your code and it still is not working, I really dont know what is wrong. AidanThDev 9 — 7y

Answer this question