Hi I have this local script which checks if the player joined and has a certain bool value which changes the visible of the gui. I don’t know why, but for some reason this isn’t working. I don’t get any errors either.
local plr = game.Players.LocalPlayer
game.Players.PlayerAdded:Connect(function()
if plr.hiddenstats.BackgroundSound.Value == false then
script.Parent.BackgroundOff.Visible = true
script.Parent.BackgroundOn.Visible = false
print(plr.hiddenstats.BackgroundSound.Value)
end
if plr.hiddenstats.BackgroundSound.Value == true then
script.Parent.BackgroundOff.Visible = false
script.Parent.BackgroundOn.Visible = true
print(plr.hiddenstats.BackgroundSound.Value)
end
end)
1 Like
This only works once, you need to use the .Changed
event to check if a value has changed and do something accordingly
local plr = game.Players.LocalPlayer
local backgroundSound = plr:WaitForChild("hiddenstats").BackgroundSound
local backgroundOff = script.Parent.BackgroundOff
local backgroundOn = script.Parent.BackgroundOn
backgroundSound.Changed:Connect(function(val)
if not val then
backgroundOff.Visible = true
backgroundOn.Visible = false
else
backgroundOff.Visible = false
backgroundOn.Visible = true
end
print(val)
end)
Also wait, you’re not exactly setting the event to the player who just joined, this only works for the localplayer, you don’t need the PlayerAdded event in this case if it’s in a Local script. And there’s a few optimizations y ou could do here and ther elike variables and what not
@JackscarIitt I know it’s very minor but why are you not using the return for .Changed
to check if it’s true or false
1 Like
Use the Value.Changed
event for that, it’ll save you so much more time
local plr = game.Players.LocalPlayer
local BackgroundSound = plr:WaitForChild("hiddenstats").BackgroundSound
BackgroundSound.Changed:Connect(function()
if BackgroundSound.Value == false then
script.Parent.BackgroundOff.Visible = true
script.Parent.BackgroundOn.Visible = false
print(BackgroundSound.Value)
else
script.Parent.BackgroundOff.Visible = false
script.Parent.BackgroundOn.Visible = true
print(BackgroundSound.Value)
end
end)
You mean the parameter of the .Changed Event? Idk if I’m being honest, just only knew about that until now
It’s not working. I’m going to explain more. I have a kind of settings gui with a button in it with this script.
local sound = game.Workspace.Sounds.BackgroundSound
local player = game.Players.LocalPlayer
local BackOnrem = game.ReplicatedStorage.SettingsRemoteEvent.BackOn
local BackOffrem = game.ReplicatedStorage.SettingsRemoteEvent.BackOff
script.Parent.MouseButton1Click:Connect(function()
if sound.Volume == 0 then
script.Parent.Parent.Parent.ClickEffect:Play()
script.Parent.Parent.BackgroundOn.Visible = true
script.Parent.Parent.BackgroundOff.Visible = false
BackOnrem:FireServer()
sound.Volume = 0.5
else
sound.Volume = 0
script.Parent.Parent.Parent.ClickEffect:Play()
script.Parent.Parent.BackgroundOn.Visible = false
script.Parent.Parent.BackgroundOff.Visible = true
BackOffrem:FireServer()
end
end)
The server script that receives the remotevent.
local event1 = game.ReplicatedStorage.SettingsRemoteEvent.BackOn
local event2 = game.ReplicatedStorage.SettingsRemoteEvent.BackOff
local function remote1(player)
local v = player.hiddenstats.BackgroundSound
v.Value = true
end
event1.OnServerEvent:Connect(remote1)
local function remote2(player)
local v = player.hiddenstats.BackgroundSound
v.Value = false
end
event2.OnServerEvent:Connect(remote2)
I have worked with remotevents then my datastore works because the server receives the data. This script above works perfectly. I’ve been working with prints to see if the value there really changes and it works. Only the problem is… if the player enters the game again, it resets the gui again. The value of the player is actualy saved only the gui is not saved and I tried to create a script above and it does not work.
1 Like
So wait, can’t you combine .Changed
and regular code? so it does the check once on the player existing and again when the value of the thing changes?
local plr = game.Players.LocalPlayer
local backgroundSound = plr:WaitForChild("hiddenstats").BackgroundSound
local backgroundOff = script.Parent.BackgroundOff
local backgroundOn = script.Parent.BackgroundOn
local function conditionCheck(val)
if not val then
backgroundOff.Visible = true
backgroundOn.Visible = false
else
backgroundOff.Visible = false
backgroundOn.Visible = true
end
print(val)
end
conditionCheck(backgroundSound.Value)
backgroundSound.Changed:Connect(function(val)
conditionCheck(val)
end)
Or am I not understanding you clearly?
1 Like
I don’t think you understand me. I will briefly summarize what I need: When the player enters the game and the script checks that his BackgroundSound boolvalue is true it makes BackgroundOn visible and makes BackgroundOff invisible. If the boolvalue is false it will do the other way around it makes the BackgroundOff visible and BackgroundOn invisible.
Ohhh! Okay now I understand you, I think you’d have to do that in a server script and get the Guis to disable via their PlayerGui. Here’s how you could do it (script in ServerScriptService)
game.Players.PlayerAdded:Connect(function(plr)
local sound = plr:WaitForChild("hiddenstats").BackgroundSound
local plrGui = plr:WaitForChild("PlayerGui")
local off = plrGui.(Location of BackgroundOff)
local on = plrGui.(Location of BackgroundOn)
if not sound.Value then
off.Visible = true
on.Visible = false
else
off.Visible = false
on.Visible = true
end
print(sound.Value)
end)
Just replace (Location of BackgroundOff)
and (Location of BackgroundOn)
with the locations of both of those inside of PlayerGui, you can basically copy their position in StarterGui to get their locations. Just make sure not to accidentally reference StarterGui haha
local off = plrGui.MainGui.Settingsframe.BackgroundOff
local on = plrGui.MainGui.Settingsframe.BackgroundOn
I got 1 problem. I get an error it says: MainGui is not a valid member of PlayergUi “Players.OficialAjoeb.PlayerGui”. Everthing is located good is it maybe because the gui isn’t loaded yet? How do I fix this.
Did you write PlayerGui
incorrectly considering the error wrote it as PlayergUi
? What is your code right now?
Yes I wrote it correctly. This is the current code:
game.Players.PlayerAdded:Connect(function(plr)
local sound = plr:WaitForChild("hiddenstats").BackgroundSound
local plrGui = plr:WaitForChild("PlayerGui")
local off = plrGui.MainGui.Settingsframe.BackgroundOff
local on = plrGui.MainGui.Settingsframe.BackgroundOn
if not sound.Value then
off.Visible = true
on.Visible = false
else
off.Visible = false
on.Visible = true
end
print(sound.Value)
end)
Maybe you need to do a WaitForChild?
local settings = plrGui:WaitForChild("MainGui").Settingsframe
local off = settings.BackgroundOff
local on = settings.BackgroundOn
This is what I’ve been trying to do for a really long time.
Here is the code that I believe is going to work:
local boolvalue = --wherever your value is
local player = game.Players.LocalPlayer
local playergui = LocalPlayer.PlayerGui
local screenGui = PlayerGui:WaitForChild("MainGui")
local settingsFrame = screenGui.SettingsFrame
local on = settingsFrame.BackgroundOn
local off = settingsFrame.BackgroundOff
local function makeGUIVisible()
if bool1.Value == true then
off.Visible = false
on.Visible = true
else
off.Visible = true
on.Visible = false
end
end
bool1.Changed:Connect(makeGUIVisible)
--Of course you can change up the function to make it different.
Updated the script, but the indentation isn’t that good as I wrote this in studio.
Also I’m pretty sure you meant something else in the function, just made it for an example. You can change the function and it will pretty much work the same.
Got the warning: Infinite yield possible on WaitForChild(“MainGui”)
That’s indeed a warning, but it’s like an exception in other programming languages. It checks for it forever unless the script finds it. Don’t mind that.
Let me give a quick example in Python:
try:
print(test) --The variable doesn’t exist
except:
print(“The code didn’t work!”)
okay excuse me what
How’s it infinitely yielding when it should exist by then? Did you remember to put this in a regular script? Could you check your player instance ingame?
It is going to infinitely yield if it doesn’t exist. If it does exist it will not.
Yes I know that but I don’t understand why it’s infinitely yielding when it should exist shortly after their player is added to the game?
It’s a warning. The warning comes every time you run it. It doesn’t say it doesn’t exist.
Edit: It’s a warning, it’s just saying if it doesn’t exist it will yield.
The warning will still lie, but the screenGui
variable should continue throughout its thread of code once it’s found
You should probably add a print()
statement after screenGui
is defined
Not error that’s a warning am I colorblind or something