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

NPC Variable becomes nil when NPC respawns ?

Asked by 4 years ago

I'm working on displaying a UI(User Interface) when an NPC BOSS dies, It does work at first but when the NPC dies again the UI doesn't show up. I think It's because the variable that holds the NPC character model becomes nil because It respawns. I've tried to place the new NPC into the same variable that holds the NPC character, but I had no success.

I use two scripts for this UI notification, one script is in the StarterCharacterScripts and the other is a local script located in the StarterPlayerScripts. The StarterCharacterScripts script checks the health of the NPC and changes a bool value located in the local player. Then the local script from the StarterPlayerScripts checks any changes from the bool value and depending if true or false it would show the UI.

Here's some code

01-- Located in the StarterCharacterScripts
02 
03local Spawn_Alert = player:WaitForChild("Spawn_Alert")
04local T = Spawn_Alert:WaitForChild("Treasure")
05 
06NPC = workspace:WaitForChild("NPC")
07NPC_Humanoid = NPC:FindFirstChild("Humanoid")
08if NPC_Humanoid.Health <= 0 then
09    T.Value = true
10else
11    NPC = workspace:WaitForChild("NPC")
12    NPC_Humanoid = NPC:FindFirstChild("Humanoid")
13    T.Value = false
14end
01-- located in the StartPlayerScripts
02 
03local Players = game:GetService("Players")
04local player = Players.LocalPlayer or Players:GetPropertyChangedSignal("LocalPlayer"):wait()
05player.CharacterAdded:Connect(function(character)
06    player.CharacterAppearanceLoaded:Wait()
07    --// Wait for player's Folder and Values
08    local Spawn_Alert = player:WaitForChild("Spawn_Alert")
09    local T = Spawn_Alert:WaitForChild("Treasure")
10    --// Get's player Image Holders
11    local T_IH = Players.LocalPlayer.PlayerGui.Alert.T_IH
12    --// Checks Value change to change Image Transparency
13    T:GetPropertyChangedSignal("Value"):Connect(function()
14        if T.Value == true then
15            T_IH.ImageTransparency = 0
16        else
17            T_IH.ImageTransparency = .7
18        end
19    end)
20end)

Just to clarify, the problem Is the NPC variable and I'm not sure how to fix this. Thanks for reading :D

0
:c AK47GOLDgun 9 — 4y

Answer this question