.Changed event only working once

Hello,

I made a script that lets you press E to break your leg. Your HRP gets anchored and you can’t move. I use a .Changed event to detect when a BoolValue changes inside the Character. It works fine once, but does not work after I do it once. I’ve used print statements in the code and have deduced that the issue comes from the .Changed event. Here is the code.

(uis script)

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		print("pressed e")
		game:GetService("ReplicatedStorage").legBroke:FireServer()
	end
end)

(server script to recieve event)

game:GetService("ReplicatedStorage").legBroke.OnServerEvent:Connect(function(player)
	print("e recieved")
	player.Character.legBroken.Value = true
end)

(leg break, this is in startercharacterscripts)

local legBroken = Instance.new("BoolValue")
legBroken.Parent = script.Parent
legBroken.Value = false
legBroken.Name = "legBroken"

legBroken.Changed:Connect(function(value)
	print("changed")
	if value == true then
		print("leg broke")
		script.Parent.HumanoidRootPart.Anchored = true
		script.Parent.Torso["Left Hip"].Enabled = false
	end
end)

(leg fix, again in startercharacterscripts)

local clickDetector = Instance.new("ClickDetector")
clickDetector.Parent = script.Parent
local isDoctor = Instance.new("BoolValue")
isDoctor.Parent = script.Parent
isDoctor.Name = "isDoctor"
isDoctor.Value = true

clickDetector.MouseClick:Connect(function(playerWhoClicked)
	if isDoctor.Value == true then
		playerWhoClicked.Character:FindFirstChild("Humanoid").Health += 30
		clickDetector.Parent:FindFirstChild("Humanoid").Health += 30
		clickDetector.Parent.Torso["Left Hip"].Enabled = true
		clickDetector.Parent.HumanoidRootPart.Anchored = false
		print("leg fixed")
	else
		print("you no qualify")
	end
end)

The only thing that prints the second time is “e recieved”

Does anyone have any idea what could be causing this, or does anyone have an alternative to the .Changed event that might work better?

Thanks.

Alternate for .Changed

legBroken:GetPropertyChangedSignal("Value"):Connect(function()
    print("Value changed!")
end)
1 Like

maybe the issue is that when you’re setting the boolValue to false, you’re doing it using a local script. This won’t replicate, so this .Changed on ServerSide won’t detect it
Then, since it won’t replicate, when you fire the remote to have the server change it back to true, it’s already true, so it doesn’t change

1 Like

The only LocalScript I use is for the UIS script, also it works completely fine once.

in that case, you’re never setting the value besides when setting the value to true

Oh wait I’m really stupid. I forgot to include the script that turns it back to false! I am going to edit the post.

I think you’re missing the line where you set the boolValue’s value to false when unanchoring the humanoidRootPart

1 Like