BoolValue doesn't register as true

Ok so I have some code for my game that requires a BoolValue to be ticked. When it is ticked, nothing prints even though the event was fired.

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer

local character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local head = character:WaitForChild("Head")
local distanceMin
local distance
local closestPlayer


UserInputService.InputBegan:Connect(function (input, IsTyping)
	if input.KeyCode == Enum.KeyCode.E and IsTyping == false then
		distanceMin = math.huge
		for _, player in pairs(Players:GetPlayers()) do
			if player ~= LocalPlayer then
				local iscrawling = player:WaitForChild("iscrawling")
				if iscrawling.Value == true then
				distance = player:DistanceFromCharacter(head.Position)
				if distance < distanceMin then
					distanceMin = distance
					closestPlayer = player
	                print(closestPlayer.Name .. " is the closest player.")
				end
			end
		end
	end
		if distanceMin < 10 then 
			print(closestPlayer.Name .. " is the closest player.")
		end
	end
end)

This is a local script in StarterGui.

Try using a function when the BoolValue is true, use the BoolValue’s .Changed event and connect with the function

Sort of like this

iscrawling.Changed:Connect(function() -- here it is
				distance = player:DistanceFromCharacter(head.Position)
				if distance < distanceMin then
					distanceMin = distance
					closestPlayer = player
	                print(closestPlayer.Name .. " is the closest player.")
				end
			end)

Is the boolvalue directly parented to the player or in a folder?

Try putting a print over here:

local iscrawling = player:WaitForChild("iscrawling")
				if iscrawling.Value == true then
                                    -- here
				distance = player:DistanceFromCharacter(head.Position)
				if distance < distanceMin then
					distanceMin = distance
					closestPlayer = player
	                print(closestPlayer.Name .. " is the closest player.")
				end

and maybe try changing this:

if distance < distanceMin then

It is directly parented to the player

When I put the print after the bool check it doesn’t print

Then it must mean the value is false.

But I check in game the value is true

Is it stuck to true? the boolvalue? if it is then you need to set it false again inside the function right after when the function is done

You should use :GetPropertyChangedSignal("Value"):Connect(function() instead

1 Like

You don’t need to use :GetPropertyChangedSignal("Value") for ValueBase objects since they have a modified .Changed event that only fires when Value property changes.

How did you set the value? Did you set it in a script, local script, or manually? I had a problem where I set a value in a local script so it was registered as false by the server, which may or may not be the problem.

I set it with a local script and another local script is checking if it is true

If a player changes the value on their client, it will not replicate to other clients.

That’s probably the problem then. If you set it in a local script it won’t replicate to other clients or the server so you would need to use a server script to set the value (or send a remote to the server to set the value).

But if I set it with the server, all of the players will have it to true

If you change the value in that player then It will be true for them.

Yeah, just set the value for the specific player.