So this is something you don't understand about the basic nature of programming. Once you run a script or any program, it runs once, and does exactly what you program it to do, then it finishes and stops running.
So, there are a few ways you can go about doing this:
LOOPS
In Lua (not an acronym btw, don't capitalize it) there are two common ways to loop:
(I'm going to use your code to show examples of how to solve your problem, all are valid, but not all are the best solution)
While Loop:
local hRP = game.Players.LocalPlayer.Character.HumanoidRootPart
local sG = game.Players.LocalPlayer.PlayerGui.screenGui
while true do
if sG.game.Visible then
hRP.Anchored = true
elseif not sG.game.Visible then
hRP.Anchored = false
end
wait()
end
The loop above will run until the condition "true" becomes false, and because I am feeding it true directly, it will run forever and never stop unless you disable the whole script.
For Loop:
local hRP = game.Players.LocalPlayer.Character.HumanoidRootPart
local sG = game.Players.LocalPlayer.PlayerGui.screenGui
for i=1,10,1 do
if sG.game.Visible then
hRP.Anchored = true
elseif not sG.game.Visible then
hRP.Anchored = false
end
end
A for loop works differently than a while loop in that it usually will not be able to loop indefinitely, and is normally used to loop through a table or a specific range as showed above. You could read the above for loop as "for i equals 1, until i equals 10, add 1 each time you run the code in the loop". So in this case, it will run until i=10 and stop. You could technically get it to run forever by subtracting 1 from i each time, but that defeats the purpose of this kind of loop and a while loop would be a much better choice for that use.
EVENTS
So those are loops, the only reason you would need to use a loop in your case here is because you are trying to find the value of a property directly, but thankfully, Lua has other ways of finding out if a property has changed:
An Event Listener, or more commonly known on Roblox as just an "Event".
Events allow you to sort of ask the game engine to run a block of code when something happens. So it will be listening behind the scenes at all times until you disconnect the event, and because of this, it requires no looping, and is quite simple:
local hRP = game.Players.LocalPlayer.Character.HumanoidRootPart
local sG = game.Players.LocalPlayer.PlayerGui.screenGui
sG:GetPropertyChangedSignal("Visible"):Connect(function()
if sG.game.Visible then
hRP.Anchored = true
elseif not sG.game.Visible then
hRP.Anchored = false
end
end
So the event above is probably the best solution to your problem because it will only fire (the term used when an event detects what it was listening for) when the specified property changes and it because of how specific this event is, it won't need to pass anything to the function it contains.
However, usually, when events fire, they pass something to the function within the event connection, and you would be able to assign that to a variable in the function arguments like so:
local hRP = game.Players.LocalPlayer.Character.HumanoidRootPart
local sG = game.Players.LocalPlayer.PlayerGui.screenGui
sG.Changed:Connect(function(property)
if property == "Visible" and sG.game.Visible then
hRP.Anchored = true
elseif property == "Visible" and not sG.game.Visible then
hRP.Anchored = false
end
end
So you can see that the property is assigned to the "property" variable in the function, and this allows you to also have more functionality in said function because not only will this event now fire when the visibility changes, it will also fire when any other property of the gui changes. Also because of that, to get what you want, you then need to add more arguments to the if statements within the function as shown.
With all that being said, there are a few more ways you can solve your problem, but they're a bit too advanced at your current skill level, and honestly shouldn't even be used in this case anyway, so I am leaving them for you to discover on your own.
You can read up on a few more ways to do loops here:
https://developer.roblox.com/en-us/api-reference/lua-docs/Lua-Globals
And, the documentation on the property changed event here:
https://developer.roblox.com/en-us/api-reference/function/Instance/GetPropertyChangedSignal
If this answers your question, then remember to mark it as the accepted solution, and one last tip for you:
If you get confident enough, I recommend you browse a few questions here and try answering questions you don't know the answer to, in order to discover new things about the language you might not have learned if you went off and did your own thing.
3 comments
Your script will only work once since the code that make the HumanoidRootPart anchored is ran once only. You need to connect a function to check for the screenGui visible property changing.
Don't worry, I'm writing up a lengthy explanation of a few things you're missing and possible solutions you could explore.
I wanted to answer too but i'm just to lazy so i gave an comment for the solution. Hopefully he figures it out. haha Thanks for helping him though xD. Also can't wait for what you have crafted for him / her.