BoolValue doesnt change

basically, a server script adds a “Stunned” value to every player:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Char)
		local StunValue = Instance.new("BoolValue")
		StunValue.Value = false
		StunValue.Name = "Stunned"
		StunValue.Parent = Char
		
	end)
end)

and i have a tool where once it hits a player, it should set the “Stunned” value to true:

	newHitbox.OnHit:Connect(function(hit, humanoid)
		
		local Stunned = humanoid.Parent.Stunned


if KnockoutChance ~= 1 then
			Stunned = true
			BodyVelocityStun.Parent = humanoid.Parent.HumanoidRootPart
			game.Debris:AddItem(BodyVelocityStun, 3)
			task.wait(3)
			Stunned = false
		elseif KnockoutChance == 1 then
			Stunned = true
			coroutine.wrap(function()
				task.wait(1.1)
				BodyFallSound:Play()
			end)()
			KnockoutFall.Stopped:Connect(function()
				KnockoutLoop:Play()
				task.wait(7)
					KnockoutRecover.Stopped:Wait()

					Stunned = false
				end)
				
			end
print(Stunned)

(that isnt the actual code, obviously. its partial)

and the code that detects it:

local Stunned = Char:WaitForChild("Stunned").Value



	game.Players.PlayerAdded:Connect(function(player)
		player.CharacterAdded:Connect(function(Character)
			game:GetService("RunService").Heartbeat:Connect(function()
				Char.Humanoid:MoveTo(Character.HumanoidRootPart.Position)
			local magu = (Char.HumanoidRootPart.Position-Character.HumanoidRootPart.Position).Magnitude
print(Stunned)
			if magu < 5 and not Stunned then
--attacking code (this is an ai)
			end
			end)
		end)
	end)

all of these scripts are server scripts

in the script that changes the value, there is a print statement, and it works. when stunned is true, it prints true, and when its false, it prints false.
however, in the other script that detects if its not true, it always detects the “Stunned” as false.

so the script that detects if it isnt stunned should actually detect if it is or not, but instead it always detects it as false

1 Like

You are supposed to check within the function because right now it is working perfectly fine

local Stunned = Char:WaitForChild("Stunned")

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
		game:GetService("RunService").Heartbeat:Connect(function()
			Char.Humanoid:MoveTo(Character.HumanoidRootPart.Position)
			local magu = (Char.HumanoidRootPart.Position Character.HumanoidRootPart.Position).Magnitude
			print(Stunned)
			if magu < 5 and not Stunned.Value then
			--attacking code (this is an ai)
			end
		end)
	end)
end)

You define the variable as false from the beginning.

I just removed the .Value

local Stunned = Char:WaitForChild("Stunned")

and placed it within your conditional so that you retrieve the value when it is needed

if magu < 5 and not Stunned.Value then

doesnt work, and no errors. ive also tried putting local Stunned = Char:WaitForChild("Stunned") inside the loop

Actually just noticed

local Stunned = Char:WaitForChild("Stunned")

Is just outside the CharacterAdded event how is char defined ill fix my example

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
		local Stunned = Character:WaitForChild("Stunned")
		RunService.Heartbeat:Connect(function()
			Character.Humanoid:MoveTo(Character.HumanoidRootPart.Position)
			local magu = (Character.HumanoidRootPart.Position -Character.HumanoidRootPart.Position).Magnitude
			print(Stunned)
			if magu < 5 and not Stunned.Value then
			--attacking code (this is an ai)
			end
		end)
	end)
end)
1 Like

still not working

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
		local Stunned = Character:WaitForChild("Stunned")
		RunService.Heartbeat:Connect(function()
			Char.Humanoid:MoveTo(Character.HumanoidRootPart.Position)
			local magu = (Char.HumanoidRootPart.Position-Character.HumanoidRootPart.Position).Magnitude
			--print(Stunned)
			if magu < 5 and not Stunned.Value then
			--attacking code (this is an ai)

also Char is the ai and Character is obviously the player who joins, so i changed the code a little

1 Like

Just quickly read through your code. You probably need to set the .Value property instead of changing the variable.

Change all lines like this:

Stunned = false

to

Stunned.Value = false

What your code current does is set a variable named “Stunned” to the BoolValue, then sets the variable to false or true, instead of setting the BoolValue’s .Value property.

1 Like

just realized that, still didnt fix it though, and no errors

this is my current code:

detects if value is not true (the ai)

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
		local Stunned = Character:WaitForChild("Stunned")
		RunService.Heartbeat:Connect(function()
			Char.Humanoid:MoveTo(Character.HumanoidRootPart.Position)
			local magu = (Char.HumanoidRootPart.Position-Character.HumanoidRootPart.Position).Magnitude
			--print(Stunned.Value)
			if magu < 5 and  Stunned.Value == false then
			--attacking code (this is an ai)
			end
			end)
		end)
	end)

the tool that sets the values:

newHitbox.OnHit:Connect(function(hit, humanoid)
		
		local Stunned = humanoid.Parent.Stunned.Value


if KnockoutChance ~= 1 then
			Stunned = true
			BodyVelocityStun.Parent = humanoid.Parent.HumanoidRootPart
			game.Debris:AddItem(BodyVelocityStun, 3)
			print("Stun, Not Knockout")
			humanoid:TakeDamage(15)
			HitByBat:Play()
			task.wait(3)
			Stunned = false
		elseif KnockoutChance == 1 then
			Stunned = true
			humanoid:TakeDamage(25)
			BodyVelocityKnockout.Parent = humanoid.Parent.HumanoidRootPart
			KnockoutFall:Play()
			BodyFallSound.Parent = humanoid.Parent
			coroutine.wrap(function()
				task.wait(1.1)
				BodyFallSound:Play()
			end)()
			KnockoutFall.Stopped:Connect(function()
				KnockoutLoop:Play()
				task.wait(7)
				KnockoutRecover:Play()
				KnockoutRecover:AdjustSpeed(.4)
				KnockoutLoop:Stop()
				StandUpSound.Parent = humanoid.Parent
				StandUpSound:Play()
					KnockoutRecover.Stopped:Wait()
					game.Debris:AddItem(BodyVelocityKnockout, .1)
					Stunned = false
				end)
				
			end

		--end  
			
			
		print(Stunned)
	end)
1 Like

some context:

this code right here (the tool that sets the value) sets the value to true/false, and it prints true:

newHitbox.OnHit:Connect(function(hit, humanoid)
		
		local Stunned = humanoid.Parent.Stunned


if KnockoutChance ~= 1 then
			Stunned.Value = true
			print(Stunned.Value)
			BodyVelocityStun.Parent = humanoid.Parent.HumanoidRootPart
			game.Debris:AddItem(BodyVelocityStun, 3)
			print("Stun, Not Knockout")
			humanoid:TakeDamage(15)
			HitByBat:Play()
			task.wait(3)
			Stunned.Value = false
			print(Stunned.Value)
		elseif KnockoutChance == 1 then
			Stunned.Value = true
			print(Stunned.Value)
			humanoid:TakeDamage(25)
			BodyVelocityKnockout.Parent = humanoid.Parent.HumanoidRootPart
			KnockoutFall:Play()
			BodyFallSound.Parent = humanoid.Parent
			coroutine.wrap(function()
				task.wait(1.1)
				BodyFallSound:Play()
			end)()
			KnockoutFall.Stopped:Connect(function()
				KnockoutLoop:Play()
				task.wait(7)
				KnockoutRecover:Play()
				KnockoutRecover:AdjustSpeed(.4)
				KnockoutLoop:Stop()
				StandUpSound.Parent = humanoid.Parent
				StandUpSound:Play()
					KnockoutRecover.Stopped:Wait()
					game.Debris:AddItem(BodyVelocityKnockout, .1)
				Stunned.Value = false
				print(Stunned.Value)
				end)
				
			end

	end)

however, this script that detects if the value is false, always detects the Stunned value as false:

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
		local Stunned = Character:WaitForChild("Stunned")
		RunService.Heartbeat:Connect(function()
			Char.Humanoid:MoveTo(Character.HumanoidRootPart.Position)
			local magu = (Char.HumanoidRootPart.Position-Character.HumanoidRootPart.Position).Magnitude
			print(Stunned.Value)
			if magu < 5 and  Stunned.Value == false then
			--attacking code (this is an ai)
			end
			end)
		end)
	end)

so the code should set the value to false, then the other scripts detects if its false or not, if its false, then it can continue code, if not, dont continue code and wait until is true

(both of the scripts are server scripts)

Was going to reply to the other response but it looks like you fixed the stunned.value already, gonna mention it anyways just in case.


Try to always remember that saving Stunned.Value rather than Stunned, will only save its current value and not the property.

So rather than

Stunned = Stunned.Value

if Stunned == true then

you want to do

Stunned = Stunned

if Stunned.Value == true then

As far as the rest of the code you provided looks for me, it should work. Make sure you didn’t do that on the rest of the script, as it might be the reason why it doesn’t work, and make sure you didn’t mix true with false or something like that.


Highly doubt this is why it doesn’t work but, try to use the .Changed event instead of RunService

local Stunned = Character:WaitForChild("Stunned")

Stunned.Changed:Connect(function()
--attacking code (this is an ai)
end)
2 Likes

ok so instead of local Stunned = humanoid.Parent.Stunned you think i should put local Stunned = humanoid.Parent.Stunned.Value?

and then change the Stunned.Value = true to Stunned = true?
or Stunned = false

and then for the script that detects the change in value, you also think that for this part:

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
		local Stunned = Character:WaitForChild("Stunned")
		RunService.Heartbeat:Connect(function()
			Char.Humanoid:MoveTo(Character.HumanoidRootPart.Position)
			local magu = (Char.HumanoidRootPart.Position-Character.HumanoidRootPart.Position).Magnitude

			if magu < 5 and  Stunned.Value == false then

i put a Stunned = Stunned inside the heartbeat loop? (kinda confusing so if you could just post the edits you think would work inside of my code that would help)

the reason for the RunService loop is so that the magu or magnitude stays updated

No, exactly the opposite. That’s why I mentioned you had fixed it.

local Stunned = humanoid.Parent.Stunned
Print("Stunned.Value")
--Will return the current value of stunned

Local Stunned = humanoid.Parent.Stunned.Value
Print("Stunned")
--Will always return the value stunned had when you made the variable, and it will never update

Ah I see, my bad. Shouldn’t be an issue if that’s the case.

well my code was already like that?:

local Stunned = humanoid.Parent.Stunned
	local Stunned = Character:WaitForChild("Stunned")
		RunService.Heartbeat:Connect(function()
			Char.Humanoid:MoveTo(Character.HumanoidRootPart.Position)
			local magu = (Char.HumanoidRootPart.Position-Character.HumanoidRootPart.Position).Magnitude

			if magu < 5 and  Stunned.Value == false then


so when they get hit by the bat, you can see they get stunned, and thats when Stunned gets set to true. but as you can see, they can still attack, but they shouldnt be able too because Stunned is true

1 Like

Can’t say for sure but it looks like you are not disconnecting the RunService event after the AI hits you. I don’t really know how you have set it up but that might be the issue and not the values themselves.

You could try something like…

local Run --Empty variable
Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(Character)
		local Stunned = Character:WaitForChild("Stunned")
		Run = RunService.Heartbeat:Connect(function()
			Char.Humanoid:MoveTo(Character.HumanoidRootPart.Position)
			local magu = (Char.HumanoidRootPart.Position-Character.HumanoidRootPart.Position).Magnitude
			print(Stunned.Value)
			if magu < 5 and  Stunned.Value == false then
				--attacking code (this is an ai)
			else --If you were stunned
				if Run then --If AI started moving
					Run:Disconnect()
				end
			end
		end)
	end)
end)

If that’s not the issue i’m afraid I can’t come up with a solution.

1 Like

well that didnt work so do you mind if i just dm you my entire system/scripts?

i redid alot of things and solved it myself

1 Like