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 3 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

-- Located in the StarterCharacterScripts

local Spawn_Alert = player:WaitForChild("Spawn_Alert")
local T = Spawn_Alert:WaitForChild("Treasure")

NPC = workspace:WaitForChild("NPC")
NPC_Humanoid = NPC:FindFirstChild("Humanoid")
if NPC_Humanoid.Health <= 0 then
    T.Value = true
else
    NPC = workspace:WaitForChild("NPC")
    NPC_Humanoid = NPC:FindFirstChild("Humanoid")
    T.Value = false
end
-- located in the StartPlayerScripts

local Players = game:GetService("Players")
local player = Players.LocalPlayer or Players:GetPropertyChangedSignal("LocalPlayer"):wait()
player.CharacterAdded:Connect(function(character)
    player.CharacterAppearanceLoaded:Wait()
    --// Wait for player's Folder and Values
    local Spawn_Alert = player:WaitForChild("Spawn_Alert")
    local T = Spawn_Alert:WaitForChild("Treasure")
    --// Get's player Image Holders
    local T_IH = Players.LocalPlayer.PlayerGui.Alert.T_IH
    --// Checks Value change to change Image Transparency
    T:GetPropertyChangedSignal("Value"):Connect(function()
        if T.Value == true then
            T_IH.ImageTransparency = 0
        else
            T_IH.ImageTransparency = .7
        end
    end)
end)

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 — 3y

Answer this question