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

Why does this script breaks when the character dies?

Asked by 5 years ago

It is a localscript inside the StarterPlayerScript which detects NPCs. But it breaks and doesn't detect anyone after a player dies.

local NPCS = {"Father Zhang Yixin", "Innkeeper GuoXin", "Fisherman LiuCan", "Mother ZhangJunyan"}
local player = game.Players.LocalPlayer
repeat wait() until workspace:FindFirstChild(player.Name) ~= nil
local character = player.Character
local Humanoid = character.Humanoid
repeat wait() until character:FindFirstChild("Humanoid")

character.Humanoid.Changed:connect(function()
    for i,v in pairs(workspace:GetChildren())do
        for a,k in pairs(NPCS)do
            if v.Name == k then
                local A = v.HumanoidRootPart.CFrame.p
                local B = character.HumanoidRootPart.CFrame.p
                local max = 10
                if (A-B).magnitude <= max then
                    v.EGui.Enabled = true
                    script.CurrentNPC.Value = v.Name
                    script.InDistance.Value = true
                    local Dialog = player.PlayerGui.NPCInteraction
                    game:GetService("UserInputService").InputBegan:Connect(function(key)
                        if key.KeyCode == Enum.KeyCode.F then
                            if v.EGui.Talking.Value == false and v.Name == script.CurrentNPC.Value and script.InDistance.Value == true then
                            v.EGui.Talking.Value = true
                            v.EGui.Walked.Value = false
                            Dialog.Frame:TweenPosition(UDim2.new(0.242,0,0.725,0),"Out","Quart",1)
                            local Text = v.EGui.Message
                            local NPCName = v.EGui.NPCName
                            local RealText = Text.Value
                            local NPCNames = NPCName.Value
                            for i=1,#RealText do
                                if v.EGui.Walked.Value ~= true then
                                Dialog.Frame.TextLabel.Text = string.sub(RealText,1,i)
                                Dialog.Frame.NPCName.Text = string.sub(NPCNames,1,i)
                                wait(0.01)
                            else
                                   break
                                end
                            end
                            if v.EGui.Walked.Value == false then
                            Dialog.Frame.Left.BackgroundTransparency = 0
                            Dialog.Frame.Left.Text = Text.Answer1.Value
                            Dialog.Frame.Left.Todo.Value = Text.Todo.Value
                            Dialog.Frame.Left.Todo.NPC.Value = Text.Todo.NPC.Value
                            Dialog.Frame.Right.BackgroundTransparency = 0
                            Dialog.Frame.Right.Text = Text.Answer2.Value
                            Dialog.Frame.Right.Todo.Value = Text.Todo.Value
                            Dialog.Frame.Right.Todo.NPC.Value = Text.Todo.NPC.Value
                            end
                            else
                                local v = workspace:FindFirstChild(script.CurrentNPC.Value)
                            end
                        end
                    end)
                else
                    if v.Name == script.CurrentNPC.Value then
                        local Text = v.EGui.Message
                        local Dialog = player.PlayerGui.NPCInteraction
                        v.EGui.Enabled = false  
                        Dialog.Frame.Position = UDim2.new(2.242,0,0.725,0)
                        Dialog.Frame.Left.BackgroundTransparency = 1
                        Dialog.Frame.Left.Text = ""
                        Dialog.Frame.Left.Todo.Value = ""
                        Dialog.Frame.Left.Todo.NPC.Value = ""
                        Dialog.Frame.Right.BackgroundTransparency = 1
                        Dialog.Frame.Right.Text = ""
                        Dialog.Frame.Right.Todo.Value = ""
                        Dialog.Frame.Right.Todo.NPC.Value = ""
                        Dialog.Frame.TextLabel.Text = ""
                        v.EGui.Talking.Value = false
                        v.EGui.Walked.Value = true
                        script.CurrentNPC.Value = ""
                        script.InDistance.Value = false
                    break
                    end             
                end
            end
        end
    end
end)
0
You need to re-create the event when the NPC respawns. AlphaGamer150 101 — 5y

Answer this question