How do I make a variable for StarterGui's children?

I am making an IsSeated script and I want a localscript in StarterGui only work when the player is seated, I think it would work if I could disable the script in StarterGui when the player is not sitting. This is the code so far:

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

function onSeated(isSeated, seat)
	if isSeated then
		--I want to achive something like this: StarterGui.LocalScript.Enabled = true  
	else
		--StarterGui.LocalScript.Enabled = false 
	end
end
humanoid.Seated:Connect(onSeated)

The script above is a LocalScript in StarterPlayer>StarterCharacterScripts
The script in startergui is an UIS system that plays music id’s for certain keys (a piano in this case)
I have tried to enable and disable the sound itself but I couldn’t find a good property for that.
Any help would be appreciated.

The LocalScript will be running in each player’s PlayerGui, not the game’s StarterGui.

game.Players:GetPlayerFromCharacter( character ) is likely to be of help to get the Player instance.

Then you can either:

  1. Navigate to their PlayerGui and disable/enable the LocalScript if you still want to do that.

  2. Call the Sound:Stop() and Sound:Play() methods on the Sounds in question.

Though my personal recommendation would be to adapt your original LocalScript that actually responds to UIS input to check game.Players.LocalPlayer.Character.Humanoid.Sit instead of messing around with enabling and disabling things externally.

1 Like

Another way you can is to get the Localplayer since you stated that it is a LocalScript and get the player gui from there and then the script to disable. What can be done as well is the addition of these lines

local player = game:GetService("Players").LocalPlayer
local plrGui = player:WaitForChild("PlayerGui")

local uisScript = plrGui.LocalScript

Where LocalScript i sthe name of the LocalScript to disable, you can disable it from there and it should work I believe Although I think You may have to make it a server script and change some of the code around to amke it work for a server script if the code is still running even after being disabled locally

1 Like

Thank you for your reply.
I don’t really understand how I can place the IsSeated script in StarterGui because it would only work in StarterCharacterScripts (according to the DeveloperHub)
And for the game.Players:GetPlayerFromCharacter( character ), do I have to make a variable for the Players and would that part go after the if isSeated then ?

I believe he meant something like this

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local player = game:GetService("Players"):GetPlayerFromCharacter(character) --Addition of this

And then you have to make variables to reference the PlayerGui and the localscript to disable and then write the disabling/enabling code in the onSeated function

1 Like