Bool.changed not working correctly?

So i got this script that when a bool value changes from true to false, vise versa, itll do something, but it works wrong and just ends up doing it 100 times and not even stop on the right one? I hope this makes sense lol

wait(1)
local park = script.Parent.InPark

script.Parent.InPark.Changed:Connect(function()
	print("got parkevent")
	if park.Value == true then --park
	script.Parent.Center.Anchored = true
	park.Value = false
		print("anchor center")
	else
		if park.Value == false then --unpark
			script.Parent.Center.Anchored = false
			park.Value = true
			print("unanchored center")
		end
		end
	end)

The changed function actually returns the new value so I suggest you format it like this;

wait(1)
local park = script.Parent.InPark

park.Changed:Connect(function(NewValue)
	print("got parkevent")
	if NewValue == true then --park
	script.Parent.Center.Anchored = true
	park.Value = false
		print("anchor center")
	elseif NewValue == false then --unpark
			script.Parent.Center.Anchored = false
			park.Value = true
			print("unanchored center")
		end
	end)

else I see nothing wrong with your script.

The .Changed event fires each time the value changes, and you’re changing the value inside the function, so that’s probably causing it to loop forever.

1 Like

You are right, how would i fix this?

Why do you need to set park to false here, if its supposed to be parked? Try removing
park.Value = false and park.Value = true and just change the value with some other method

i tried this, but i dont think this works.

wait(1)
local park = script.Parent.InPark
dog = true

script.Parent.InPark.Changed:Connect(function(NewValue)
	print("got parkevent")
	if dog == true then --park
		script.Parent.Center.Anchored = true
		park.Value = false
		dog = false
		print("anchor center")
	elseif dog == false then --unpark
			script.Parent.Center.Anchored = false
		park.Value = true
		dog = true
			print("unanchored center")
		end
	end)

Youre basically doing the same thing here. Are you trying to make a parking system for a car? You could change park with a button instead of switching the value right after parking and unparking. Or if you want to anchor it and quickly unanchor it, you could simply do that without changing the value.

yeah basically i made it so when the driver gets out, this is what happens, so its basically just anchoring and unanchoring it, but i have no clue where else to start

You could get rid of the variable entirely and just anchor it and unanchor it quickly.

DriverLeftCar:Connect(function(car)
car.PrimaryPart.Anchored = true
wait()
car.PrimaryPart.Anchored = false
)

(you can set up custom events with BindableEvent | Roblox Creator Documentation)

So you could fire this event every time the driver leaves the car without needing to use a variable.

I basically just changed the part that changed the value of the park to the part of the script that anchored the center, works fine

Well, hopefully i was able to help, glad you figured it out.

1 Like