Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

My NPC Death Sequence Script won't work?

Asked by 5 years ago

After this NPC is killed, it is supposed to turn into rust and then explode. It won't work for some reason and it isn't giving me anything on output. Any help?

01script.Parent:WaitForChild("Humanoid").Health.Changed:Connect(Check)
02 
03function Check()
04if script.Parent.Humanoid.Health <= 0 then
05    local Children = script.Parent:GetChildren()
06    if Children:IsA("BasePart") or ("UnionOperation") then
07        Children.Material = Enum.Material.CorrodedMetal
08        Children.BrickColor = BrickColor.new("Cork")
09    end
10        elseif script.Parent:WaitForChild("Humanoid").Health >= 0 then
11        print("boss is chilling")
12    end
13end
0
Correction: It did give me something in the output: 23:16:13.450 - Workspace.Tutorial_Miniboss.DeathSequence:1: attempt to index field 'Health' (a number value) Daemonophobiaa 37 — 5y

1 answer

Log in to vote
1
Answered by
crywink 419 Moderation Voter
5 years ago
Edited 5 years ago

Hey!

Your problem here is that you're trying to connect the Changed event to a property rather than an instance. Changed is actually connected to the instance itself and the connected function will be called with the property's key as its parameter. A code example is shown below...

1function Check(param)
2    if param == "Health" then
3        -- your code here
4    end
5end
6 
7humanoid.Changed:Connect(Check)

Alternatively, you can use the GetPropertyChangedSignal method where you can connect changed signals to certain properties. It works exactly like the Changed event, although it will only be fired when the specified property changes, eliminating the need for an if statement to make sure the changed property is the one you want. A code example for that is also shown below...

1-- I'll break this down so it's easier to understand.
2local event = humanoid:GetPropertyChangedSignal("Health") -- An event is returned, we just need to connect it.
3 
4event:Connect (function() -- Connecting the event defined above.
5    -- your code here
6end)

Apart from that, I also see that you're trying to invoke IsA on Children, which is a table value. :GetChildren() returns a table with all children of said instance. You'll have to iterate through Children with a for loop or find a singular instance by its index. (e.g. Children[1])

If you have any other questions, feel free to ask.

If this helped, please remember to accept the answer. :)

0
Alright I'll try and see if it works Daemonophobiaa 37 — 5y
0
First example doesn't work for some reason. Nothing about it on the output. I'll try to do the second example Daemonophobiaa 37 — 5y
0
Both examples don't work for some reason.. Nothing on the output as well Daemonophobiaa 37 — 5y
0
How are you using it? If you would like to contact me on discord, it's Samuel#0440. crywink 419 — 5y
0
Alright, I contacted you on Discord Daemonophobiaa 37 — 5y
Ad

Answer this question