Script prints BoolValue is false even when it's true

I want to make an AFK button.
When you join, I want to clone a BoolValue into your Humanoid & make it false.
An AFK TextButton will appear on the screen crossed out by an ImageLabel. When you click it, I want the BoolValue to change to true & the cross to disappear. So you are AFK, but if you click it again, I want the BoolValue to be false and the cross to reappear.

I’ve made a script in ServerScriptService to teleport the player if the AFK BoolValue is false but do nothing if the AFK BoolValue is true. It always prints that it is false even when it isn’t. Here is the script that doesn’t work:

function teleportPlayers() -- This script doesn't work

	local players = game:GetService("Players"):GetChildren() -- Gets all Players
	
for i = 1, #players do -- Loops through them

local AFK = players[i].Character.Humanoid.AFK

if AFK.Value == true then -- Doesn't work even if the BoolValue is true

			print("You are AFK")
			
			--Do Nothing

elseif AFK.Value == false then -- Always runs

			print("You are not AFK")
			
			--Teleport Player
			
	end
	end
end

wait(15)

teleportPlayers() -- Runs Function

I’ve looked for solutions on the Developer Hub, I’ve watched YouTube videos & I’ve tried changing the script multiple times.

Here is the script in ServerScriptService that puts the BoolValue into the players Humanoid when they load in:

game.Players.PlayerAdded:Connect(function(player) -- This script works
	player.CharacterAdded:Connect(function(character)
		
		if player.character:FindFirstChild("Humanoid") then -- Checks that it's a player
			
			local rep = game:GetService("ReplicatedStorage")
			
			local Value = rep:WaitForChild("AFK"):Clone() -- Clones BoolValue in ReplicatedStorage
			
			local Humanoid = player.character:FindFirstChild("Humanoid")
			
			Value.Parent = Humanoid -- Puts the BoolValue into the players Humanoid
			
			Value.Value = false -- Makes the BoolValue false so the player isn't AFK
			
			local playerGui = player:WaitForChild("PlayerGui")
			
			local X = playerGui:WaitForChild("AFKGui"):WaitForChild("Frame"):WaitForChild("ImageLabel")
			
			X.Visible = true -- Crosses out words AFK on the players screen
			
			end
		end)
	end)

Here is the LocalScript (inside a TextButton) that changes the BoolValue in the players Humanoid when the button is clicked:

script.Parent.MouseButton1Click:Connect(function(click) -- This script works
	
	local player = game.Players.LocalPlayer
	local Humanoid = player.character:WaitForChild("Humanoid")
	local X = script.Parent.Parent:WaitForChild("ImageLabel")
	local AFKvalue = Humanoid:WaitForChild("AFK")
	
	if player.character:WaitForChild("Humanoid") then
		
		if X.Visible == true then -- If there's a cross
			
			X.Visible = false -- then get rid of the cross
			
			AFKvalue.Value = true -- and make them AFK
			
			print("You are now AFK")
			
		elseif X.Visible == false then -- If there's not a cross
			
			X.Visible = true -- then cross out AFK
			
			AFKvalue.Value = false -- they aren't AFK
			
			print("You are no longer AFK")
			
		end
	end
end)

I’m extremely new to scripting so please let me know if you spot any errors. Any help would be appreciated! Thanks in advance

This is because the LocalScript is changing the value locally, so the server will not see the change. To solve this you will need to use a RemoteEvent to change the value on the server.

2 Likes

Ok, Thanks I didn’t expect to get a reply so quickly! I’ll try that out!

It’s working, thank you so much!

1 Like