#Problem spawning npc's

171 messages · Page 1 of 1 (latest)

obsidian condor
#

local spawns = script.Parent
local spawnTime = 20
local maxZombies = 10
local currentTime = 0
local isNightTime = false
local zombieCount = 0

local function checkIfNight()
return currentTime >= 18 or currentTime <= 7
end

local function updateZombieCount()
zombieCount = 0
for _, obj in pairs(workspace:GetChildren()) do
if obj.Name == "Zombie" and obj:IsA("Model") then
zombieCount = zombieCount + 1
end
end
end

while true do
wait(spawnTime)
updateZombieCount()
isNightTime = checkIfNight()

if isNightTime and zombieCount < maxZombies then
    for _, spwn in pairs(spawns:GetChildren()) do
        if spwn:IsA('BasePart') then
            local zombieCopy = game.ReplicatedStorage['Zombie']:Clone()
            zombieCopy.Parent = game.Workspace
            zombieCopy.HumanoidRootPart.CFrame = CFrame.new(spwn.Position + Vector3.new(0, 3, 0))
        end
    end
end

end

#

try this

fading orbit
#

wow tysm

halcyon shaleBOT
#

studio** You are now Level 1! **studio

obsidian condor
fading orbit
#

quick question. howd you learn scripting. i can understand it but i dont know how to put any of it together to actually put it to good use

obsidian condor
#

wdym

halcyon shaleBOT
#

studio** You are now Level 1! **studio

obsidian condor
#

watching vids

#

buying courses

#

etc

fading orbit
#

any specific vids?

#

or just channels in generla?

obsidian condor
#

channels

fading orbit
#

this is the script im using, am i using the wrong property?

#

local Lighting = game:GetService("Lighting")

while true do
wait(0.01)
Lighting.ClockTime = (Lighting.ClockTime + 0.01)

if Lighting.ClockTime >= 6 and Lighting.ClockTime < 18 then
    
   
else

end

end

obsidian condor
#

uhm

#

lemme see

fading orbit
#

your script is using currentTime but mine is clocktime

#

do i need to change that?

obsidian condor
#

you dont

fading orbit
#

bc they are spawning just not at night

#

they are spawning on the timer but also at day

obsidian condor
#

local spawns = script.Parent
local spawnTime = 20
local maxZombies = 10
local zombieCount = 0

local function checkIfNight()
local clockTime = game.Lighting.ClockTime
return clockTime >= 18 or clockTime <= 7
end

local function updateZombieCount()
zombieCount = 0
for _, obj in pairs(workspace:GetChildren()) do
if obj.Name == "Zombie" and obj:IsA("Model") then
zombieCount = zombieCount + 1
end
end
end

while true do
wait(spawnTime)
updateZombieCount()

if checkIfNight() and zombieCount < maxZombies then
    for _, spwn in pairs(spawns:GetChildren()) do
        if spwn:IsA('BasePart') then
            local zombieCopy = game.ReplicatedStorage['Zombie']:Clone()
            zombieCopy.Parent = game.Workspace
            zombieCopy.HumanoidRootPart.CFrame = CFrame.new(spwn.Position + Vector3.new(0, 3, 0))
        end
    end
end

end

#

nowit should spawn eithin 6 and 7

fading orbit
#

okay ill try it

#

now it is not spawning any at all

#

im sorry

#

i am trying to figure it out too but my knowledge is limited

obsidian condor
#

ok wait

fading orbit
#

dont worry

#

i solved it

#

thank you

obsidian condor
#

its fixed

#

what you di

fading orbit
#

if checkIfNight() and zombieCount < maxZombies then
for , spwn in pairs(spawns:GetChildren()) do
if spwn:IsA('BasePart') then

zombieCount = 0
for , obj in pairs(workspace:GetChildren()) do
if obj.Name == "Zombie" and obj:IsA("Model") then
zombieCount = zombieCount + 1

no _ after the for in both of these sections

halcyon shaleBOT
#

studio** You are now Level 2! **studio

fading orbit
#

thank you

obsidian condor
#

nw

fading orbit
#

also how do you make the dead bodies of the humanoids not damage the player?

obsidian condor
#

wdym

#

ah

#

for which code

fading orbit
#

for the zombie

#

i can send you the script

obsidian condor
#

ye send

fading orbit
#

local NPC = script.Parent
local debounce = false

NPC.NPCHumanoid.Touched:Connect(function(hit)
if hit and game.Players:GetPlayerFromCharacter(hit.Parent) then
if debounce == false then
debounce = true
hit.Parent.Humanoid:TakeDamage(10)
wait(.5)
debounce = false
end
end
end)

function FindPlayer(Position)
local List = game.Workspace:GetChildren()
local Torso = nil
local Distance = 300
local HumanoidRootPart = nil
local Humanoid = nil
local Player = nil

for i = 1, #List do
    Player = List[i]
    if (Player.ClassName == "Model") and (Player ~= script.Parent) then
        HumanoidRootPart = Player:FindFirstChild("HumanoidRootPart")
        Humanoid = Player:FindFirstChild("Humanoid")
        if (HumanoidRootPart ~= nil) and (Humanoid ~= nil) and (Humanoid.Health > 0) then
            if (HumanoidRootPart.Position - Position).Magnitude < Distance then
                Torso = HumanoidRootPart
                Distance = (HumanoidRootPart.Position - Position).Magnitude
            end
        end
    end
end
return Torso

end

while true do
wait(1)
local Target = FindPlayer(script.Parent.HumanoidRootPart.Position)
if Target ~= nil then
script.Parent.NPCHumanoid:MoveTo(Target.Position, Target)
end
end

this is the AIScript

#

just taking a guess but check if local.Health = 0

#

then im not sure what next

#

somehow make the humaoid do no damage

obsidian condor
#

local NPC = script.Parent
local debounce = false

NPC.NPCHumanoid.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid and humanoid.Health > 0 and not debounce then
debounce = true
humanoid:TakeDamage(10)
wait(0.5)
debounce = false
end
end
end)

function FindPlayer(position)
local closestDistance = 300
local closestTorso = nil

for _, player in pairs(game.Players:GetPlayers()) do
    local character = player.Character
    local humanoid = character and character:FindFirstChild("Humanoid")
    local humanoidRootPart = character and character:FindFirstChild("HumanoidRootPart")
    
    if humanoid and humanoidRootPart and humanoid.Health > 0 then
        local distance = (humanoidRootPart.Position - position).Magnitude
        if distance < closestDistance then
            closestDistance = distance
            closestTorso = humanoidRootPart
        end
    end
end
return closestTorso

end

while true do
wait(1)
local target = FindPlayer(NPC.HumanoidRootPart.Position)
if target then
NPC.NPCHumanoid:MoveTo(target.Position, target)
end
end

halcyon shaleBOT
#

studio** You are now Level 2! **studio

obsidian condor
#

so basically

#

i msdeit to check if the himanoid has above 0 helath eg the zombie hasabove 0 health beforedoing damage

fading orbit
#

okay ty

#

thanks for explaining too

obsidian condor
#

nw

fading orbit
#

umm i just tested the script and its not working

#

sorry

#

also how do i make the npc's roam when they are no targeting a player and not within a certain distance of a player

obsidian condor
#

idk i dont see an error with he script

fading orbit
#

either do i

#

maybe capitalisation?

obsidian condor
#

yes maybe

fading orbit
#

the name of the actual humanoid is NPCHumanoid

obsidian condor
#

or maybe error with other of ypir code

fading orbit
#

do i need to change anythig to the actaul name?

fading orbit
obsidian condor
#

the code i sent needs configuring by you

fading orbit
#

in what way?

obsidian condor
#

names

#

placing it in the correct place

fading orbit
#

oh

#

what do i need to change?

#

im sorry

obsidian condor
#

Make sure the debounce mechanism is working correctly. Sometimes a lot of aggressive debouncing can prevent the code from executing

fading orbit
#

im not sure how

#

can u explain in detail?

obsidian condor
#

idk if i can explain it in detail

#

but basically

fading orbit
#

im not familar with how the debounce mechanism works also

#

this is just pasted and edited code

obsidian condor
#

that could be the issue

#

if the stuff isnt in the right place

#

eg

#

does your npc have a humanoidrootpart

fading orbit
#

from this video

fading orbit
obsidian condor
#

move to function?

#

shpuld be calledon the

fading orbit
#

not sure

obsidian condor
#

humanoid part

#

notnpc humanoid

fading orbit
#

explain please?

#

oh

#

the ai script and amin script rely's on it to be called NPCHUmanioid

#

so what do i have to change in the script you gave me for it to work?

#

change the humanoid parts to NPChumanoid?

halcyon shaleBOT
#

studio** You are now Level 3! **studio

obsidian condor
#

ok i chnagedthe debounce

#

local NPC = script.Parent
local debounce = false

NPC.Humanoid.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid and humanoid.Health > 0 and not debounce then
debounce = true
humanoid:TakeDamage(10)
wait(0.5)
debounce = false
end
end
end)

function FindPlayer(position)
local closestDistance = 300
local closestTorso = nil

for _, player in pairs(game.Players:GetPlayers()) do
    local character = player.Character
    local humanoid = character and character:FindFirstChild("Humanoid")
    local humanoidRootPart = character and character:FindFirstChild("HumanoidRootPart")

    if humanoid and humanoidRootPart and humanoid.Health > 0 then
        local distance = (humanoidRootPart.Position - position).Magnitude
        if distance < closestDistance then
            closestDistance = distance
            closestTorso = humanoidRootPart
        end
    end
end
return closestTorso

end

while true do
wait(1)
local target = FindPlayer(NPC.HumanoidRootPart.Position)
if target then
NPC.Humanoid:MoveTo(target.Position, target)
end
end

#

and also

#

i searched this up

#

In Roblox, NPCs typically have a Humanoid component named just "Humanoid". If your NPC's humanoid is named "NPCHumanoid", keep it as is; otherwise, change NPC.NPCHumanoid to NPC.Humanoid.

fading orbit
#

okay

#

thankyou

#

now i just needa figure out how to change value of leaderstats after detecting that the player has killed a zombie .🙄🙄

obsidian condor
#

i can code it for you

halcyon shaleBOT
#

studio** You are now Level 3! **studio

brisk garden
fading orbit
#

sure, also the script is not working 😒😒

obsidian condor
#

error message ?

fading orbit
#

no error msg

#

this is how i have my npc setup in workspace

#

what needs to be changed to NPCHumanoid and what just needs to be humanoid

#

bc i changed the blue to NPC.NPCHUmanoid

#

instead of NPC.Humanoid

obsidian condor
winged compass
#

change NPCHumanoid to HumanoidRootPart or smth

fading orbit
#

okay

obsidian condor
fading orbit
#

current script still not working 😦

obsidian condor
#

i dont know now

#

youll have to wait for someone else to explain

winged compass
fading orbit
#

are you able to teach me how to change the leaderstats value?

fading orbit
winged compass
winged compass
fading orbit
#

i meant when i kill a zombie for it to detect it and update the leaderstats

winged compass
#

change this to NPC.NPCHumanoid.Health > 0

fading orbit
#

okay ty

obsidian condor
winged compass
#

well the thing is

#

you have to detect the player that killed the zombie

#

which means that you have to modify ur tools that kill the zombie

fading orbit
#

im aiming to use multiple different melee weapons, rn i have a sword in the starter pack

#

but am plainning to add a shop ui to buy different melees

winged compass
#

what you could do

#

is add an owner value to the zombie

#

and make the tool change the value when it hits the zombie

fading orbit
#

would that not only display hits tho not kills?

winged compass
fading orbit
#

and the other parts?

winged compass
fading orbit
#

yeah

#

need to figure out how to do that first lmao

#

im barely a scripter

#

just learning

#

@winged compass @obsidian condor how do i add a roaming script to the AIScrpt?

winged compass
fading orbit
#

for the npc to wlak around when it is not within distnace of a pplayer

halcyon shaleBOT
#

studio** You are now Level 4! **studio

winged compass
#

could you send me ur script so i can modify it

fading orbit
#

local NPC = script.Parent
local debounce = false

NPC.HumanoidRootPart.Touched:Connect(function(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid and NPC.NPCHumanoid.Health > 0 and not debounce then
debounce = true
humanoid:TakeDamage(5)
wait(0.5)
debounce = false
end
end
end)

function FindPlayer(position)
local closestDistance = 150
local closestTorso = nil

for _, player in pairs(game.Players:GetPlayers()) do
    local character = player.Character
    local humanoid = character and character:FindFirstChild("Humanoid")
    local humanoidRootPart = character and character:FindFirstChild("HumanoidRootPart")

    if humanoid and humanoidRootPart and humanoid.Health > 0 then
        local distance = (humanoidRootPart.Position - position).Magnitude
        if distance < closestDistance then
            closestDistance = distance
            closestTorso = humanoidRootPart
        end
    end
end
return closestTorso

end

while true do
wait(1)
local target = FindPlayer(NPC.HumanoidRootPart.Position)
if target then
NPC.NPCHumanoid:MoveTo(target.Position, target)
end
end

#

this is the current script i have

#

and also my humanoid is named NPCHumanoid

#

ty

winged compass
#

local NPC = script.Parent
local debounce = false

NPC.HumanoidRootPart.Touched:Connect(function(hit)
    local player = game.Players:GetPlayerFromCharacter(hit.Parent)
    if player then
        local humanoid = hit.Parent:FindFirstChild("Humanoid")
        if humanoid and NPC.NPCHumanoid.Health > 0 and not debounce then
            debounce = true
            humanoid:TakeDamage(5)
            wait(0.5)
            debounce = false
        end
    end
end)

function FindPlayer(position)
    local closestDistance = 150
    local closestTorso = nil

    for _, player in pairs(game.Players:GetPlayers()) do
        local character = player.Character
        local humanoid = character and character:FindFirstChild("Humanoid")
        local humanoidRootPart = character and character:FindFirstChild("HumanoidRootPart")

        if humanoid and humanoidRootPart and humanoid.Health > 0 then
            local distance = (humanoidRootPart.Position - position).Magnitude
            if distance < closestDistance then
                closestDistance = distance
                closestTorso = humanoidRootPart
            end
        end
    end
    return closestTorso
end

while true do
    wait(1)
    local target = FindPlayer(NPC.HumanoidRootPart.Position)
    if target then
        NPC.NPCHumanoid:MoveTo(target.Position, target)
    else
        NPC.NPCHumanoid:MoveTo(NPC.HumanoidRootPart.Position + Vector3.new(math.random(-10,10),0,math.random(-10,10)))
    end
end
#

@fading orbit