While loop ignores Bool Value's Conditions changed by a Local Script

Hello, basically, i made a Character Script that changes a Bool value inside the character.
bandicam 2023-11-05 17-54-41-249
bandicam 2023-11-05 17-55-05-104

At the same time, theres a Server Script which detects if the Bool Value is True. If that’s the case, it will start a while loop.
bandicam 2023-11-05 17-57-00-709

However, the while loop ignores the bool value’s conditions. Likely because the value’s being changed by a Local Script, but i don’t know a good solution to that. How can i set up a good remote event for this? or anything else that can solve it? thanks!

ServerScript

poop = script.Parent:WaitForChild("isrunning")
local sounds = script.Parent:GetChildren()
local filtersnd = {}
for i=1,#sounds do
	if sounds[i].Name == "run" then
		table.insert(filtersnd, sounds[i])
	end
end
while task.wait(0.5) do
	if poop.Value == true then
filtersnd[math.random(1,#filtersnd)]:Play()
end
end

Local Script

local humanoid = script.Parent:WaitForChild("Humanoid")
player = script.Parent:WaitForChild("Tors")

humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if humanoid:GetState() ~= Enum.HumanoidStateType.Freefall then
		if humanoid.WalkSpeed ~= 0 then
			if humanoid.MoveDirection.Magnitude > 0 then
				if player.Waist["Helmet"]["isrunning"].Value == false then 
					player.Waist["Helmet"]["isrunning"].Value = true
					
				end
			else
				player.Waist["Helmet"]["isrunning"].Value = false
				
			end
		end
	end
end)

You’re already firing a remote event to the server to detect when they’re running or not, so just set player.Waist["Helmet"]["isrunning"].Value on the function that receives that remote event.

1 Like

I’m sorry, but the event is not visible in the screenshot. Also, attach the event triggering and changing the BoolValue to the server script, or just determine the value when the event is fired, and start the loop if the condition is true

1 Like

You can also do this without BoolValue by using a local variable in the script:

poop = script.Parent:WaitForChild("isrunning")
local Event = script.Parent:FindFirstChild("Event")
local BoolValue = false
local sounds = script.Parent:GetChildren()
local filtersnd = {}
for i=1,#sounds do
	if sounds[i].Name == "run" then
		table.insert(filtersnd, sounds[i])
	end
end
Event.OnServerEvent:Connect(function(Bool)
	if Bool == true then
		BoolValue = true
	elseif Bool == false then
		BoolValue = false
	end
end)
while task.wait(0.5) do
	if BoolValue == true then
		filtersnd[math.random(1,#filtersnd)]:Play()
	end
end
1 Like

Sorry about that, i forgot to remove the Remote Event lines since it was a solution i tried earlier.

Actually, I’m pretty sure you can just change that LocalScript to a normal script and it will still work fine + it’s server-sided now.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.