Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why do BoolValue's value not update?

Hey! I'm here again -- with another question:

So , I started debugging using the Lua debugger at if wander==false and hit. blalblalba it works fine, but when the function is activated multiple times (which doesnt do anything besides activating since canenable is false), the code abruptly stops. Alright, that's the first "error". (Not the main question,however it comes first in the script so I thought I'll show it first)

local wander = test.wanderstate.Value
wander = false -- MOB STATE
canenable = true
local enabler = script.Parent.Parent.Cave.enabler

enabler.Touched:Connect(function(hit)

	if wander == false and hit.Parent:FindFirstChild("Humanoid") and canenable == true
		then wander = true
		canenable = false
		wait()
		print(wander)
		wait(3)
		canenable = true
		return wander
		end
end)

Second "error" - the main question: Why do BoolValue's value not update with script? when I set the BoolValue of wander to true manually, this code runs fine :

if wander == true then
	print("initiate")
	loadedInactive:Stop()
	loadedActivate:Play()
	wait(6)
while wander == true do
	loadedWalk:Play()
	moveto = Vector3.new((math.random(-30,30)+test.Torso.Position.X),0,(math.random(-30,30)+test.Torso.Position.Z))
	
	test.Humanoid:MoveTo(moveto) -- MOVE TO RANDOM COORDINATES ABOVE
	test.Humanoid.MoveToFinished:Wait(5)
	for i=1,3 do
		if math.random(1,3) == 3 then
			loadedSwing:Play()
		end
		if wander == false then break 
		else
			loadedWalk:Stop()
			loadedIdle:Play()
			wait(5) -- COOLDOWN
			loadedIdle:Stop()
		end
	end 

but when BoolValue is set to true using the first .Touched function, the script does not activate,even though when I put in print(wander) in the function and it says true.

1 answer

✓ Accepted answer

Hello, asgm!

Sorry it took so long for some to respond to you, I just woke up. Anyways, I've changed the first script up it's just like your old one but instead I renamed your debounce to "debounce" and didn't call .Value on the first line. You would just have to change the location to the part and BoolValue.TrueOrFalse is for the BoolValue and TouchPart is for the part that is getting touched.

local TrueOrFalse = script.Parent:WaitForChild("Value")
local debounce = false

local TouchPart = script.Parent

TouchPart.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and TrueOrFalse.Value == false and debounce == false then
		TrueOrFalse.Value = true
		debounce = true

		wait(3)
		
		TrueOrFalse.Value = false
		debounce = false

	end
end)

For this one I made a LocalScript inside of StarterPlayerScripts, you have to change the variables around once again.

local Player = game.Players.LocalPlayer
local Char = workspace:WaitForChild(Player.Name)
local Humanoid = Char:WaitForChild("Humanoid")

local TouchPart = workspace:WaitForChild("Part")
local TrueOrFalse = TouchPart:WaitForChild("Value")

TrueOrFalse.Changed:Connect(function(NewValue)
	if NewValue == true then
		-- Your code here
	else
		-- If NewValue isn't true
	end
end)

1 comment

0

Thanks! Your TrueOrFalse.Changed did the trick :)

This discussion is preserved as it was. New answers, comments, and votes are closed.