Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Loop waits for value to change, but when value changes, loop doesn't end? What's wrong with my code?

Asked by 1 year ago

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.

1 answer

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

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 RemoteEvents 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)
0
Thank you so much! I'll try to incorperate RemoteEvents into more of my game :) hoppyman360 -13 — 1y
0
Uh oh, it seems like everything stopped working lol hoppyman360 -13 — 1y
0
Uh oh, it seems like everything stopped working lol, I think the game is waiting for the Child RemoteEvent in line 6 of the LocalScript and is not running any other code hoppyman360 -13 — 1y
0
Are you sure you created a RemoteEvent inside ReplicatedStorage? If yes, did you change its name? If yes, change the "RemoteEvent" to the new name T3_MasterGamer 2189 — 1y
Ad

Answer this question