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

Player is in radius but npc doesn't attack, how do I fix this?

Asked by 1 year ago
Edited 1 year ago

So I want to make an NPC that follows you and when it's close enough it kills you, but for some weird reason the if statement doesn't fire. I replaced the less than sign with the greater than sign, and that seemed to work but it kills you anywhere, please help.

if (script.Parent.HumanoidRootPart.Position - v.HumanoidRootPart.Position).magnitude < 10 then
            v.Humanoid:TakeDamage(100)
                wait(1.5)
                d = false
            end

Full script:

local d = false
while wait() do
    for i, v in pairs(workspace:GetChildren()) do
        if v:FindFirstChild("HumanoidRootPart") and (script.Parent.HumanoidRootPart.Position - v.HumanoidRootPart.Position).magnitude < 1000 and v ~= script.Parent and v.Humanoid.DisplayName ~= "Duck" and v:FindFirstChild("HumanoidRootPart").Anchored ~= true and v.Humanoid.Health ~= 0 then
            if not d then
                d = true
                script.Parent.Humanoid:MoveTo(v.HumanoidRootPart.Position)
                if (script.Parent.HumanoidRootPart.Position - v.HumanoidRootPart.Position).magnitude < 6 then
                    v.Humanoid:TakeDamage(100)
                        wait(1.5)
                        d = false
                end
            end
        end
    end 
end

script.Parent.Humanoid.Died:Connect(function()
    script:Destroy()
end)
0
wdym by "but it kills you anywhere"? does that mean there are hiding spots/safe zones? T3_MasterGamer 2189 — 1y
0
No, I meant that if you are far away from him he can still kill you. And I don't want it to do that. ghostgamer90391 9 — 1y

1 answer

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

This is most likely due to you only checking the condition once.

Since your code is not in a function nor being called upon, it is only going to check the condition once when the script loads and it will not check it again.

There are two ways of going around this; that is by using a loop or an event. I would not recommend using a loop for this as it will use unnecessary resources when the player is standing still.

The Humanoid has a Running event which will fire whenever the player moves. I, however, prefer to use the method GetPropertyChangedSignal for the MoveDirection property of the Humanoid. The reason for this- is that the Running event, for some reason, fires twice when you start running. (usually not a big deal; I still rather use the MoveDirection as it seems to work better). You can use whichever you want.

local function running()
    if (script.Parent.HumanoidRootPart.Position - v.HumanoidRootPart.Position).magnitude < 10 then
        v.Humanoid:TakeDamage(100)
        wait(1.5)
        d = false
    end
end

Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()

    if Humanoid.MoveDirection ~= Vector3.new(0,0,0) then
        if not Character:GetAttribute("Running") then -- debounce
            Character:SetAttribute("Running", true)
            game:GetService("RunService"):BindToRenderStep("Moving", 1, running)
        end
    else
        game:GetService("RunService"):UnbindFromRenderStep("Moving")
        Character:SetAttribute("Running", false)
    end
end)

BindToRenderStep - UnBindFromRenderStep

0
The code is in a loop. ghostgamer90391 9 — 1y
0
Yeah my bad, I didn't read your description and was going solely based of your title and code. Can you include your full script? xInfinityBear 1777 — 1y
0
I updated the post. ghostgamer90391 9 — 1y
Ad

Answer this question