I am working on a game and I want a majority of the functionality to come from a script in a chair I am doing, but how would I change text in a GUI in screenGui from the script in the seat? It is a basic one player game, so hopefully that makes it a bit easier.
this is the script I am talking about:
Do I use remoteEvents? I am just a bit unsure on what path I should take that would be the easiest.
While you don’t necessarily need to change it on the client directly, you should aim to keep anything that deals with GUIs on the client side. And yes, you will need a remote event that fires the client to change the text. Oh, and you want to make changes to the PlayerGui, if you didn’t already know
This would go in your script that is inside your seat.
RemoteEvent:FireClient(player) --You will need something that defines the player, so maybe a GetPlayerFromCharacter() type of situation
This could go inside starter player script, or inside startergui, but it must be a local script
local player = game.Players.LocalPlayer
RemoteEvent.OnClientEvent:Connect(function()
player.PlayerGui.ScreenGui.TextLabel.Text = "Changed text" --Put whatever you want it to change to
end)
You will need to include the paths for where your remote event is stored, and not just say “RemoteEvent” like I did for the example
You could do it that way, like I said previously. But it’s better to have GUIs controlled by local scripts, since they are solely for the client to see. That doesn’t mean you can’t change it like you said, but it’s just a preference and suggestion that I’m giving out
Try using a StringValue or the value type you need in somewhere like ReplicatedStorage, then with a server script try changing the value, something like this:
local Status = game.ReplicatedStorage:WaitForChild('StringValue') -- There would be a StringValue on ReplicatedStorage
for i = 30,0,-1 do
Status.Value = 'Current time is: '.. i
end
Now that you are changing the value in the server, through a local script you will change the TextLabel text, for example:
local Status = game.ReplicatedStorage:WaitForChild('StringValue')
Status:GetPropertyChangedSignal('Value'):Connect(function() -- When Value is changed, this fires.
script.Parent.Text = Status.Value -- We change the value through the server, then we get the text from the value and finally we set the property to the value.
end)
Either if you want to change it for only one player or as you wanted then:
local seat = workspace.Seat or script.Parent
local function onTouch(hit)
local playerName = hit.Parent.Name
if game.Players:FindFirstChild(playerName) then
local Player = game.Players:GetPlayerByCharacter(hit.Parent)
Player.PlayerGui.ScreenGui.TextLabel.Text = '' -- Modify to your gui name, text, etc.
end
seat.Touched:Connect(onTouch) -- I made this really fast, however. This won't work really unless there is a check if the player is seated.
If you think that I’m wrong or have any feedback please reply to me.
You dont have to use a RemoteEvent, you can get the player more easy with a touch function for example :
local Seat = script.Parent
local function touched(getplayer)
local player = game.Players:GetPlayerByCharacter(getplayer)--That get the player that touched the part or seat
player.PlayerGui.INSERTYOURSCREENGUINAMEHERE.TextLabel.Text = ""--Text you want to change
end
Seat.Touched:Connect(touched)
Like I said in two previous replies, this way is possible without a remote event. I just find it better to change all GUIs on the client side, since it will always remain client sided. In this case, yes this works. I’ve experienced in other cases with going back and forth between controlling GUIs on the server and client can mess things up.
I am not the best at scripting but I might be able to help for this one can you show your path and full script for the text changing? you might not be running the function at all.
I think you are just not running it as I said onPlayerAdded is the name of the function I think you are getting confused with
game.Players.PlayerAdded:Connect(function(Plr)
Lets say you have your GUI Where you said.
-- Services
local Players = game:GetService("Players")
-- Local Player
local Player = Players.LocalPlayer
-- Path to the gui
local GuiPath = Player.PlayerGui:WaitForChild("Startgamegui"):WaitForChild("Frame")
-- Function to be ran
local function onPlayerAdded()
GuiPath.TextLabel.Text = "Well, we don't have much time. I am just an AI controlling a GUI."
end
-- Function is ran on player added
game.Players.PlayerAdded:Connect(function(Player)
onPlayerAdded()
end
I am not 100% sure it will work but you can give it a try