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

How do I prevent NPC ServerScripts from breaking?

Asked by 4 years ago

Introduction

I was creating some NPC's and I tested it. It works fine and all, but when I kill the NPC, it respawns and just stands there, doing nothing. I checked for any errors, and nothing showed up. I searched it up on the internet, still nothing.

Scripts

Here's my ServerScript inside of the NPC model:

local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
player.CharacterAdded:Connect(function(character)
    while not character do
        character.AncestryChanged:Wait()
    end

    local hrp = character:WaitForChild('HumanoidRootPart')
    local hum = character:WaitForChild('Humanoid')
    local enemyHum = script.Parent.EnemyHum
    local enemyHRP = script.Parent.HumanoidRootPart

    local xOffset = enemyHRP.Parent.RightUpperArm.RightShoulder.C0.X
    local yOffset = enemyHRP.Parent.RightUpperArm.RightShoulder.C0.Y
    local zOffset = enemyHRP.Parent.RightUpperArm.RightShoulder.C0.Z

    local enemyHRPcf = enemyHRP.Position
    local dead = false
    local inRange = false
    hum.Died:Connect(function()
        dead = true
    end)
    while wait() do
        if (enemyHRP.Position - hrp.Position).Magnitude < 30 then
            if hum.Health > 0 then
                inRange = true
                script.Parent.Pistol.Handle.fire:Play()
                enemyHRP.CFrame = CFrame.new(enemyHRP.Position, hrp.Position)
                enemyHRP.Parent.RightUpperArm.RightShoulder.C0 = CFrame.new(xOffset,yOffset,zOffset) * CFrame.Angles(math.rad(hrp.Orientation.X + 45),0.2,0) * CFrame.Angles(math.rad(hrp.Orientation.X + 45),0.2,0)
                local bulletPart = workspace.bullet:Clone()
                bulletPart.Parent = workspace
                bulletPart.Position = script.Parent.Pistol.Handle.Position
                bulletPart.Transparency = 0
                bulletPart.Anchored = true
                bulletPart.CanCollide = false
                bulletPart.Orientation = (script.Parent.Pistol.Handle.Orientation - hrp.Position) + Vector3.new(90,270,0)
                for i = 0,25,0.5 do
                    bulletPart.CFrame = bulletPart.CFrame * CFrame.new(0,0,i)
                    wait()
                    bulletPart.Touched:Connect(function(hit)
                        local t = true
                        if hit.Parent:WaitForChild('Humanoid') then
                            if t == true then
                                t = false
                                hum:TakeDamage(50)
                                bulletPart:remove()
                            end
                        end
                    end)
                end
                bulletPart:remove()
            elseif (enemyHRP.Position - hrp.Position).Magnitude > 30 then
                inRange = false
            end
        end
    end
end)

Conclusion

I'm very sorry if the script is hard to read, but I really need an answer. Please help!

0
Try adding a while wait(.5) in the beginning and put a break at the end? AntoninFearless 622 — 4y
0
I dont really work alot with NPCs so sorry if I am not able to help AntoninFearless 622 — 4y
0
@AntoninFearless Ah, I understand. I'll try it out anyways. Sensei_Developer 298 — 4y

Answer this question