Hello. I am attempting to make a UI open when the value of a stringvalue is equal to 1. I do this by cloning the UI into PlayerGui. However, I am currently unable to set PlayerGui as my UI’s parent, and the UI subsequently does not show up when the value matches. How may I fix this? Thank you.
gui = script.Parent.Parent.Parent.UI.CockpitUI
function Value()
gui:Clone()
gui.Parent = PlayerGui
end
if script.Parent.SeatValue.Value == 1 then
Value()
end
Are you trying to do this when the SeatValue changes or just upon first run? If when it changes, you need to connect to the function when the value changes.
local function Value(newValue) -- newValue will be script.Parent.SeatValue's new value.
if newValue == 1 then
-- clone to the PlayerGui
end
end
script.Parent.SeatValue.Changed:Connect(Value)
I am trying to make a system which will open the gui when the seatvalue is equal to 1, and close it if the seatvalue is not equal to 1. Seatvalue may change throughout the game.
I have used this script, however it does not currently open anything. What may be wrong with it?
gui = script.Parent.Parent.Parent.UI.CockpitUI
local function Value(newValue) -- newValue will be script.Parent.SeatValue's new value.
if newValue == 1 then
gui:Clone()
gui.Parent = newValue.PlayerGui
end
end
script.Parent.SeatValue.Changed:Connect(Value)
Is SeatValue like an IntValue/NumberValue etc? If so, newValue will be a number, thus it won’t have a member PlayerGui (you’d get an error along the lines of “attempt to index number with PlayerGui”). So, assuming script.Parent is a seat you should get the seat’s occupant (a humanoid), then get the player from the occupant’s parent (which will be a model)
You would have to listen to two things in this case: the occupant, and for the value to change.
So you could do something like:
local seat = script.Parent -- this would be whatever your seat is
local seatValue = seat.SeatValue -- this will be your value (IntValue or similar)
local gui = script:FindFirstAncestor('UI').CockpitUI
local activeGui -- this will be the ScreenGui inside of the player who is sat in the seat
local function onChanged()
if activeGui then
activeGui:Destroy() -- this will run always assuming activeGui is truthy (anything but false or nil, in this case a ScreenGui). this handles cleanup when a player is removed from the seat, or when the SeatValue is changed
end
local occupant = seat.Occupant
if seatValue.Value == 1 and occupant then -- if seatValue's value is 1 and the seat has an occupant,
local player = game:GetService('Players'):GetPlayerFromCharacter(occupant.Parent) -- this should get the player whose character is sitting in your seat
if player then -- there is a player for this occupant (whatever is sat in the seat isn't an NPC)
activeGui = gui:Clone()
activeGui.Parent = player.PlayerGui
end
end
end
seat:GetPropertyChangedSignal('Occupant'):Connect(onChanged) -- this will fire whenever the seat's occupant changes
seatValue.Changed:Connect(onChanged)
SeatValue is a stringvalue that can be either 0 or 1. It is set to 0 by default, and changes to 1 when a player is in a specific seat, resetting to 0 when the player exits the seat. I am trying to detect it in the script, as to open the GUI while the player is in the seat, and close it when the player leaves. The GUI is also parented to a model in the workspace, and not starter GUI. This is the main necessity of said script. The current one you provided currently gives an error.
Okay if it’s a StringValue you would need to check that seatValue is ‘1’ or ‘0’ with quotes, since a number cannot be equal to a string and vice versa.
Thank you for clarifying. I changed the stringvalue to a numbervalue, and your current script works flawlessly. Very thankful to you, saved me hours of time. Will mark the above script as Solution. Thank you.