While true do loop only printing one string

Hello!

I am trying to make a while true do loop where it checks if a value of a boolvalue is either true or false every 0.1 seconds. It prints whether if the boolvalue is either true or false, however when the game starts, the boolvalue is false (that’s correct) but, when the boolvalue turns true, it doesn’t print that the boolvalue is true, it only prints that the boolvalue is false. Any Solutions

Script:

local door = script.Parent
local doorDestroyValue = door:FindFirstChild("DoorDestroyValue")

while true do 
	if doorDestroyValue.Value == true then
		door:Destroy()
		print("Value is true")
	else
		print("Value is False")
	end
	task.wait(0.1)
end
1 Like

Make sure the script is a ServerScript or a LocalScript. Did you change the bool value on the client?

The script is a ServerScript. But i did change the boolvalue on the client in another script

you dont have to use a loop you could use .Changed or GetPropertyChangedSignal

try this version of script and see if it works

local door = script.Parent
local doorDestroyValue = door:WaitForChild("DoorDestroyValue")

doorDestroyValue:GetPropertyChangedSignal("Value"):Connect(function()
    task.wait()
    if doorDestroyValue.Value == true then 
        print("Value is true")
    else
        print("Value is false")
    end
end)
1 Like

Nope it still does not want to work. Should I try to change the script to a local script?

because doorDestroyValue is a child of door then it will get destroyed when you destroy the door which means that you will not be able to reference it

yes but the door does not even get deleted to begin with.

you added door:Destroy() in the first if statment which destroy the door

1 Like

is the value being changed on client or the server ? if its client than it would make sense why those changes are not triggered on the server

could you perhaps show the script that updates doorDestroyValue

Try running both codes in ServerScript. If the code that changes values and the code that recognizes the changed values are both server scripts, it will work

The value is being changed on the client side with another script

if the value is changed on client than those changes wont apply to server you could make a remote where client sends message to server and server updates the value

I haven’t learnt remote events yet sorry. Can you tell me how to do that?

could you send the local script that updates the value on the client so I have more understanding of how code looks like

The local script:

local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local mouse = localPlayer:GetMouse()
local DoorPart = workspace.Build.DoorPart
local DoorValue = workspace.Build.Door.DoorDestroyValue
script.Parent = localPlayer.Backpack:WaitForChild("MasterKey")


mouse.Button1Down:Connect(function()
	if mouse.Target == DoorPart then
		print("Mouse")
		DoorValue.Value = true
	end
end)
1 Like

→ Create RemoteEvent on replicated storage
→ Name it “Deletedoor” or something

→ create a script in ServerScriptService

Server Script :

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DeletedoorRemote = ReplicatedStorage:WaitForChild("Deletedoor")

function OnDoorValueChanged(DoorModel)
    if DoorModel.doorDestroyValue.Value == true then 
        print("Value is true")
     else
        print("Value is false")
     end
end 

for i,v in pairs(workspace.Build:GetChildren()) do 
 if v.Name == "Door" then 
  v.doorDestroyValue:GetPropertyChangedSignal("Value"):Connect(function()
     task.wait()
     OnDoorValueChanged(v)
    end)
  end
end


DeletedoorRemote.OnServerEvent:Connect(function(Player,DoorModel)
    if not DoorModel:FindFirstChild("DoorDestroyValue") then 
        warn("DoorDestroyValue is missing from DoorPart")
         return
    end 
    
    DoorModel.DoorDestroyValue.Value = true 
end)

LocalScript :

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DeletedoorRemote = ReplicatedStorage:WaitForChild("Deletedoor")

local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer
local mouse = localPlayer:GetMouse()
script.Parent = localPlayer.Backpack:WaitForChild("MasterKey")


mouse.Button1Down:Connect(function()
	if mouse.Target and mouse.Target.Name == "DoorPart" then
		print("Mouse")
		DeletedoorRemote:FireServer(mouse.Target)
	end
end)

you can find out how remotes work here

I got this error on the serverscript in line 14

19:42:19.422 doorDestroyValue is not a valid member of Model “Workspace.Build.Door” - Server - Script:14

could you find doorDestroyValue in the explorer and send screenshot

you can go to explorer and search for word doorDestroyValue and take a screenshot so i can see the path of where its at

image_2024-12-24_195248342
Sorry i took so long, had dinner

also that DoorDestroyValueScript was my old script that I had problems with.

its ok no worries take your time

oh it seems like i made a typo in the previous server script try this one

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DeletedoorRemote = ReplicatedStorage:WaitForChild("Deletedoor")

function OnDoorValueChanged(DoorModel)
    if DoorModel.DoorDestroyValue.Value == true then 
        print("Value is true")
     else
        print("Value is false")
     end
end 

for i,v in pairs(workspace.Build:GetChildren()) do 
 if v.Name == "Door" then 
  v.DoorDestroyValue:GetPropertyChangedSignal("Value"):Connect(function()
     task.wait()
     OnDoorValueChanged(v)
    end)
  end
end


DeletedoorRemote.OnServerEvent:Connect(function(Player,DoorModel)
    if not DoorModel:FindFirstChild("DoorDestroyValue") then 
        warn("DoorDestroyValue is missing from DoorPart")
         return
    end 
    
    DoorModel.DoorDestroyValue.Value = true 
end)