#Im trying to make a hostile mob but the mob wont chase me

25 messages · Page 1 of 1 (latest)

golden dragon
hard spruce
#

Beta game v1 @ 30 Apr 2024 16:45 auto-recovery file was created - Studio
16:45:22.850 Workspace.R6.AIscript:56: attempt to index nil with 'Character' - Server - AIscript:56
16:45:22.850 Stack Begin - Studio
16:45:22.850 Script 'Workspace.R6.AIscript', Line 56 - Studio - AIscript:56
16:45:22.850 Stack End - Studio but i get this error

golden dragon
#

If your bad at coding

#

watch brawls tut

#

👍

hard spruce
#

ok

tepid zealot
#

can you send your code like this

#

.```lua
hello world

hard spruce
tepid zealot
#

`

untold harbor
#

this is his code

#
local npc = script.Parent

-- Define speed
local speed = 10

-- Define damage amount
local damageAmount = 10  -- Adjust as needed

-- Define max health
local maxHealth = 100  -- Adjust as needed

-- Function to calculate distance between NPC and target
local function distance(pos1, pos2)
    return (pos1 - pos2).magnitude
end

-- Function to make NPC chase the target
local function chaseTarget(target)
    if target and target:IsDescendantOf(game.Workspace) then
        print("Chasing target...")
        local direction = (target.Position - npc.Position).unit
        local newPosition = npc.Position + direction * speed
        npc.Humanoid:MoveTo(newPosition)
    else
        print("Target not found or not in workspace.")
    end
end

-- Function to damage the player
local function damagePlayer(target)
    print("Damaging player...")
    local humanoid = target:FindFirstChild("Humanoid")
    if humanoid then
        humanoid:TakeDamage(damageAmount)
    end
end

-- Create a health bar above the NPC
local function createHealthBar()
    local billboardGui = Instance.new("BillboardGui")
    billboardGui.Size = UDim2.new(4, 0, 0.5, 0)
    billboardGui.Adornee = npc.Head
    billboardGui.StudsOffset = Vector3.new(0, 3, 0)

    local frame = Instance.new("Frame")
    frame.Size = UDim2.new(1, 0, 1, 0)
    frame.BackgroundColor3 = Color3.fromRGB(255, 0, 0)  -- Red color
    billboardGui.Parent = npc.Head
    frame.Parent = billboardGui
    return frame
end

-- Loop to continuously chase the target
while true do
    local target = game.Players.LocalPlayer.Character
    if target then
        print("Target found:", target.Name)
        if distance(npc.Position, target.Position) > 10 then
            chaseTarget(target)
        else
            -- Stop moving if the target is too close
            print("Stopping moving...")
            npc.Humanoid:Move(Vector3.new(0, 0, 0))
            -- Damage the player if caught
            damagePlayer(target)
        end
    ```
#
        -- Update health bar
        local healthBar = createHealthBar()
        local humanoid = npc:FindFirstChildOfClass("Humanoid")
        if humanoid then
            healthBar.Size = UDim2.new(humanoid.Health / maxHealth, 0, 1, 0)
        end
    else
        print("Target not found.")
    end
    wait(0.1)  -- Adjust the delay as needed
end```
tepid zealot
#

send me the error codes

#

@hard spruce

untold harbor
#

yo scripte

#

@tepid zealot

#

did u find a variabel named distance in that code

#

oh nvm

#

@hard spruce

#

i got no errors

#

i mean i don't see erros

tepid zealot
#

well it might be related to script placement

untold harbor
#

I don't see errors...