#I need help with my script

38 messages · Page 1 of 1 (latest)

whole wind
#

i need the npc (where the script is in) deal 15 dmg to the closest player to it but i want it to also have a range of 3 studs and play anim on attack but it doesnt seem to work

local attackAnimationId = "rbxassetid://15561118444" 
local attackRange = 2
local damageAmount = 15

function findNearestPlayer(pos)
    local players = game.Players:GetPlayers()
    local nearestPlayer = nil
    local minDistance = attackRange + 1 than attackRange
    for _, player in ipairs(players) do
        if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
            local distance = (player.Character.HumanoidRootPart.Position - pos).magnitude
            if distance < minDistance then
                nearestPlayer = player.Character.Humanoid
                minDistance = distance
            end
        end
    end
    return nearestPlayer, minDistance
end

while true do
    wait(3) 
    local nearestPlayer, distance = findNearestPlayer(script.Parent.HumanoidRootPart.Position)
    if nearestPlayer and distance <= attackRange then
        nearestPlayer.Health = nearestPlayer.Health - damageAmount
        local humanoid = script.Parent:FindFirstChild("Humanoid")
        if humanoid and humanoid:IsA("Humanoid") then
            humanoid:LoadAnimation(attackAnimationId):Play()
        end
    end
end```
please place your code like this
#

what is this variable, it is not in your code snippet
pos

sturdy berry
#

Okay, and dont mind this script i just need help fixing it cause im kinda trash 😭

#

it prob needs intense rescripting

whole wind
#

no it should work

#

but a while true with wait 3 seconds is quite long

#

and a 3 stud distance is also quite small, so you need to be hugging the npc

sturdy berry
#

it doesnt idk why..

fickle rampartBOT
#

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

whole wind
#

oh just saw pos inside the function parameter so nvm that

sturdy berry
#

it doesnt work i mean

whole wind
#

do you get any errors?

sturdy berry
#

worst part, nope

whole wind
#

try increasing the attackrange

sturdy berry
#

i did

#

oh wait

#

unable to cast value to object

whole wind
#

what line?

sturdy berry
#

idk xD

whole wind
#

also, Magnitude should be with capital M

sturdy berry
#

IMO that script needs rescripting

whole wind
#

and this is just incorrect syntax
local minDistance = attackRange + 1 than attackRange

#

if you want to do a comment do it like this
local minDistance = attackRange + 1 --than attackRange

sturdy berry
#

nah i did it like that

#

i just missed a few words

#

ok i think ik what the issue is

#

i kept it in torso 💀

#

and it needs to access humanoidrootpart

whole wind
#

ah

sturdy berry
#

check check again

#

yea cant cast value to object

#

when put normally

#

but no humanoidrootpart error

#

also anim doesnt play

#

i gotta make the anim override every other ones

#

one*

whole wind
#

i dont know much about animations

sturdy berry
#

yee