Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Trying to change ScreenGui Text?

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

✓ Accepted answer

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

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)

2 comments

0

also, I don't think you can use a property as a variable

0

You can.

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

3 comments

0

The question was how to make it work, not how to make it better.

0

Yea it should work and make it better

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.