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!
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)
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