Destroying and Adding, Value?

Basically, I have a ServerScript that changes the value of Blocking,

but this script only checks if it changes once (i think)??

I am deleting and re-adding Blocking whenever hold is true,

so how do I detect when Blocking is changed when it exists? If I do just

Character:FindFirstChild("Blocking").Changed:Connect(Blocked()) 

then it says " attempt to index nil with ‘Changed’ "

Character.ChildAdded:Connect(function(child)
	if child.Name == "Blocking" then
		Character:FindFirstChild("Blocking").Changed:Connect(Blocked()) 
	end
end)

function Blocked()
	if equipped and hold then 
              --- Do the blocking stuff here
	end
end

so how do I run a function when Blocking is changed, but with deleting and adding back Blocking???

In the line of code Character:FindFirstChild("Blocking").Changed:Connect(Blocked())
Remove the parentheses from it, because you are calling the function Blocked instead of connecting it.

It should be
Character:FindFirstChild("Blocking").Changed:Connect(Blocked)

You could make use of the ChildAdded and ChildRemoving events.

But, a better option would be just to not destroy it and find a different way around.

local MyBool = script.Parent.MyBool -- aimed at
MyBool.Changed:Connect(function()
	if MyBool.Value then
		print("The boolean value changed to true")
	end
end)
1 Like
local boolValue = script.Parent

boolValue.Changed:Connect(function(value)
    if value then
       --do code 
    end
end)
1 Like

My bad, I missed .Value (that never happens :slight_smile:)

the boolvalue will return nil because I am adding, destroying, and re-adding it

the boolvalue will return nil because I am adding, destroying, and re-adding it

It still returns the same error.
“attempt to index nil with ‘Changed’”

Odd. The order you define this matters. MyBool must be referenced/defined before the Changed:Connect(function

Actually it should error if not. You must be changing/removing then adding a new bool that is then not referenced.

1 Like

You could use the AncestryChanged or Destroying event instead.

1 Like

This is what I came up with. However putting the DB in a loop breaks it for some reason.

local DB
DB = false

Character.ChildAdded:Connect(function(child)
	if child.Name == "Blocking" then
		DB = true
	end
end)

Character.ChildRemoved:Connect(function(child)
	if child.Name == "Blocking" then
		DB = false
	end
end)

if DB == true then -- When putting this in a loop it kinda breaks the .Changed code
	Character:FindFirstChild("Blocking").Changed:Connect(function() 
		-- Code Here
	end)
end

nvm i fixed it, thanks
chars…

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.