#my script wont work

1 messages · Page 1 of 1 (latest)

rugged wraith
#

so, i was doing scripts for a zombie, i wanted to do a script that will chase the nearest player and will damage them with 10 health (i did that script with AI)
here is my script

local zombie = script.Parent
local humanoid = zombie:FindFirstChildOfClass("Humanoid")
local Players = game:GetService("Players")
local Workspace = game:GetService("Workspace")

-- Damage settings
local DAMAGE_AMOUNT = 10
local DEBOUNCE_TIME = 1 -- seconds

-- Debounce table to prevent rapid damage
local touchDebounce = {}

-- Helper function to apply damage
local function applyDamageToPlayer(hit)
    local character = hit.Parent
    if character and Players:GetPlayerFromCharacter(character) then
        local playerHumanoid = character:FindFirstChildOfClass("Humanoid")
        if playerHumanoid and playerHumanoid.Health > 0 then
            local playerId = character:GetDebugId()
            if not touchDebounce[playerId] then
                playerHumanoid.Health = playerHumanoid.Health - DAMAGE_AMOUNT
                touchDebounce[playerId] = true
                task.delay(DEBOUNCE_TIME, function()
                    touchDebounce[playerId] = nil
                end)
            end
        end
    end
end

second part in the comments

#
local partNames = {"Head", "Torso", "Left Arm", "Right Arm", "Left Leg", "Right Leg", "HumanoidRootPart"}
for i, partName in partNames do
    local part = zombie:FindFirstChild(partName)
    if part then
        part.Touched:Connect(applyDamageToPlayer)
    end
end

function getNearestPlayer()
    local nearestPlayer = nil
    local shortestDistance = math.huge
    local zombiePos = zombie:GetPivot().Position

    for i, player in Players:GetPlayers() do
        local character = player.Character
        if character and character:FindFirstChild("HumanoidRootPart") and character:FindFirstChildOfClass("Humanoid").Health > 0 then
            local charPos = character:GetPivot().Position
            local distance = (charPos - zombiePos).Magnitude
            if distance < shortestDistance then
                shortestDistance = distance
                nearestPlayer = character
            end
        end
    end
    return nearestPlayer
end

while true do
    task.wait(0.5)
    if humanoid and humanoid.Health > 0 then
        local target = getNearestPlayer()
        if target and target:FindFirstChild("HumanoidRootPart") then
            humanoid:MoveTo(target.HumanoidRootPart.Position)
        end
    end
end
#

nvm i fixed it

quaint stag
#

Carlos Spicy Wiener

echo reef
# rugged wraith ```lua local partNames = {"Head", "Torso", "Left Arm", "Right Arm", "Left Leg", ...

I recommend you dont use

while true do
    task.wait(0.5)
    if humanoid and humanoid.Health > 0 then
        local target = getNearestPlayer()
        if target and target:FindFirstChild("HumanoidRootPart") then
            humanoid:MoveTo(target.HumanoidRootPart.Position)
        end
    end
end

instead it'd be better if you had a function and bound it to RunService

local function chaseNearestPlayer()
    if humanoid and humanoid.Health > 0 then
        local target = getNearestPlayer()
        if target and target:FindFirstChild("HumanoidRootPart") then
            humanoid:MoveTo(target.HumanoidRootPart.Position)
        end
    end
end

RunService.Heartbeat:Connect(chaseNearestPlayer)

Every frame, RunService signals to the Heartbeat event, which runs all functions connected to it
Then you could also stop if if you want, or make a function you can use to turn it on or off if you want