Before I provide my code, I want to give some context.
I have a LocalScript inside of an ImageButton in a GUI. Once this button is pressed, among other functions, it sets a Bool value to true, which is inside of a part in the Workspace. (this is my way of working around BindableEvents because I'm lazy). There is another Script inside the part which has a loop that waits for the value to change.
Once the value successfully is changed by the LocalScript in the GUI, nothing is printed in the output. Does my loop not work?
Script inside of Part:
local CamBool = script.Parent.CamBool repeat wait() until CamBool.Value == true print("test")
LocalScript inside of ImageButton:
local Start = game.StarterGui.TitleGUI local Unclicked = script.Parent local Clicked = script.Parent.Parent.ButtonClicked local Title = script.Parent.Parent.Title local Blur = game.Lighting["TitleGUI blur"] local dssbool = game.StarterGui.dssbool local tutbool = game.StarterGui.tutbool local CamBool = game.Workspace.CameraPart.CamBool Unclicked.MouseButton1Click:Connect(function() Unclicked.Visible=false Clicked.Visible=true wait(0.2) Clicked.Visible=false Unclicked.Visible=true wait(0.1) Title.Visible=false Blur.Enabled=false Clicked.Visible=false Unclicked.Visible=false if dssbool.Value == true then tutbool.Value = true CamBool.Value = true print("tutbool value changed") else print("Tutorial will not be executed, because player is not new.") end end)
By the way, dssbool is a bool value that is changed if a player joins for the first time, this is to execute a tutorial. I guess I don't need the tutbool, but that's irrelevant. My problem is that even when CamBool's value is changed, "test" isn't printed.
It's because you changed it only to the client, not the server. To make it clear, if an instance (any object that exists in the game) changes in the client, for example changing a part's position, only the client (you/LocalPlayer/the computer you're using) will only see the new position and not everyone and the server. While if you do it in the server, you, everyone, and the server can see the new position.
• Solution 1:
Normal Scripts (used to) run only in the server, while Local Scripts only run in the client. Why did I say used to in Normal Scripts? It's because of the new update where Roblox added the RunContext
property. It has 3 options: Server, Legacy (basically Server in the previous updates), and Local. Local transforms a normal script to a LocalScript without changing its Instance identity/Class. Or you could just change it to a local script and everything works.
• Solution 2:
But if you want it to stay in Server, you should use RemoteEvent
s to change the Value from the client to the server. Make sure you create the RemoteEvent
inside ReplicatedStorage
.
Script inside of Part:
local ReplicatedStorage = game:GetService("ReplicatedStorage") local RemoteEvent = ReplicatedStorage.RemoteEvent local CamBool = script.Parent.CamBool RemoteEvent.OnServerEvent:Connect(function(player) CamBool.Value = true print("test") end)
LocalScript inside of ImageButton:
local Players = game:GetService("Players") local Player = Players.LocalPlayer local ReplicatedStorage = game:GetService("ReplicatedStorage") local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent") local Start = Player:WaitForChild("PlayerGui").TitleGUI -- use Player.PlayerGui instead of game.StarterGui local Unclicked = script.Parent local Clicked = script.Parent.Parent.ButtonClicked local Title = script.Parent.Parent.Title local Blur = game.Lighting:WaitForChild("TitleGUI blur") local dssbool = Player:WaitForChild("PlayerGui").dssbool local tutbool = Player:WaitForChild("PlayerGui").tutbool local CamBool = game.Workspace.CameraPart.CamBool Unclicked.MouseButton1Click:Connect(function() Unclicked.Visible = false Clicked.Visible = true task.wait(0.2) Clicked.Visible = false Unclicked.Visible = true task.wait(0.1) Title.Visible = false Blur.Enabled = false Clicked.Visible = false Unclicked.Visible = false if dssbool.Value == true then tutbool.Value = true RemoteEvent:FireServer() -- makes CamBool true print("tutbool value changed") else print("Tutorial will not be executed, because player is not new.") end end)