#Dummy (Help)

68 messages · Page 1 of 1 (latest)

polar fox
#

Hi, I made a dummy that is supposed to follow the player for 3 seconds and, after the 3 seconds, go straight back to a part called "p1", but the NPC does not go back to the part but just stops. Can anyone help me?

``
Script: local npc = script.Parent
local hrpOfNPC = npc:WaitForChild("HumanoidRootPart")

local plrsHit = {}
local maxDistance = math.huge
local chaseDuration = 3

local p1 = workspace:FindFirstChild("p1")

npc.Humanoid.Touched:Connect(function(touch)
local player = game.Players:GetPlayerFromCharacter(touch.Parent)
if player and npc.Humanoid.Health > 0 and not plrsHit[player] then
local playerCharacter = player.Character
if playerCharacter and playerCharacter:FindFirstChild("Humanoid") and playerCharacter.Humanoid.Health > 0 then
plrsHit[player] = true
playerCharacter.Humanoid:TakeDamage(20)
wait(1)
plrsHit[player] = false
end
end
end)

while wait() do
local plrs = game.Players:GetPlayers()
local closestHRP

for i, plr in pairs(plrs) do
    local playerCharacter = plr.Character
    if playerCharacter and playerCharacter:FindFirstChild("HumanoidRootPart") and playerCharacter.Humanoid.Health > 0 then
        local hrp = playerCharacter.HumanoidRootPart
        local distanceBetween = (hrpOfNPC.Position - hrp.Position).Magnitude

        if not closestHRP then
            closestHRP = hrp
        end

        if (hrpOfNPC.Position - closestHRP.Position).Magnitude > distanceBetween then
            closestHRP = hrp
        end
    end
end

if closestHRP and (hrpOfNPC.Position - closestHRP.Position).Magnitude <= maxDistance then
    npc.Humanoid:MoveTo(closestHRP.Position)
end

if npc.Humanoid.Health <= 0 then
    npc.Humanoid:MoveTo(npc.HumanoidRootPart.Position) 
    for _, player in pairs(plrs) do
        plrsHit[player] = false 
    end
    npc.Humanoid.Health = 0 
    break 
end

if closestHRP and (hrpOfNPC.Position - closestHRP.Position).Magnitude <= maxDistance then
    wait(chaseDuration)
    if p1 then
        npc.Humanoid:MoveTo(p1.Position) 
    else
        warn("Part 'p1'")
    end
end

end``

grizzled berry
#

here

#
local npc = script.Parent
local hrpOfNPC = npc:WaitForChild("HumanoidRootPart")

local plrsHit = {}
local maxDistance = math.huge
local chaseDuration = 3 

local p1 = workspace:FindFirstChild("p1")

npc.Humanoid.Touched:Connect(function(touch)
    local player = game.Players:GetPlayerFromCharacter(touch.Parent)
    if player and npc.Humanoid.Health > 0 and not plrsHit[player] then
        local playerCharacter = player.Character
        if playerCharacter and playerCharacter:FindFirstChild("Humanoid") and playerCharacter.Humanoid.Health > 0 then
            plrsHit[player] = true
            playerCharacter.Humanoid:TakeDamage(20)
            wait(1)
            plrsHit[player] = false
        end
    end
end)

while task.wait() do
    local plrs = game.Players:GetPlayers()
    local closestHRP

    for i, plr in pairs(plrs) do
        local playerCharacter = plr.Character
        if playerCharacter and playerCharacter:FindFirstChild("HumanoidRootPart") and playerCharacter.Humanoid.Health > 0 then
            local hrp = playerCharacter.HumanoidRootPart
            local distanceBetween = (hrpOfNPC.Position - hrp.Position).Magnitude

            if not closestHRP then
                closestHRP = hrp
            end

            if (hrpOfNPC.Position - closestHRP.Position).Magnitude > distanceBetween then
                closestHRP = hrp
            end
        end
    end

    if closestHRP and (hrpOfNPC.Position - closestHRP.Position).Magnitude <= maxDistance then
        npc.Humanoid:MoveTo(closestHRP.Position)
        wait(chaseDuration)
        if p1 then
            npc.Humanoid:MoveTo(p1.Position) 
        else
            warn("Part 'p1'")
        end
    end

    if npc.Humanoid.Health <= 0 then
        npc.Humanoid:MoveTo(npc.HumanoidRootPart.Position) 
        for _, player in pairs(plrs) do
            plrsHit[player] = false 
        end
        npc.Humanoid.Health = 0 
        break 
    end

end
#

this should work good

polar fox
#

not working @grizzled berry

grizzled berry
#

sad

#

wha error

polar fox
#

no error but he doesnt go back to his part

primal sedge
#

humanoid:MoveTo() sucks use tweenService

polar fox
#
local hrpOfNPC = npc:WaitForChild("HumanoidRootPart")

local plrsHit = {}
local maxDistance = 20

local p1 = workspace:FindFirstChild("p1")

local TweenService = game:GetService("TweenService")

npc.Humanoid.Touched:Connect(function(touch)
    local player = game.Players:GetPlayerFromCharacter(touch.Parent)
    if player and npc.Humanoid.Health > 0 and not plrsHit[player] then
        local playerCharacter = player.Character
        if playerCharacter and playerCharacter:FindFirstChild("Humanoid") and playerCharacter.Humanoid.Health > 0 then
            plrsHit[player] = true
            playerCharacter.Humanoid:TakeDamage(20)
            wait(1)
            plrsHit[player] = false
        end
    end
end)

while task.wait() do
    local plrs = game.Players:GetPlayers()
    local closestHRP

    for i, plr in pairs(plrs) do
        local playerCharacter = plr.Character
        if playerCharacter and playerCharacter:FindFirstChild("HumanoidRootPart") and playerCharacter.Humanoid.Health > 0 then
            local hrp = playerCharacter.HumanoidRootPart
            local distanceBetween = (hrpOfNPC.Position - hrp.Position).Magnitude

            if not closestHRP then
                closestHRP = hrp
            end

            if (hrpOfNPC.Position - closestHRP.Position).Magnitude > distanceBetween then
                closestHRP = hrp
            end
        end
    end

    if closestHRP and (hrpOfNPC.Position - closestHRP.Position).Magnitude <= maxDistance then
        local tweenInfo = TweenInfo.new(chaseDuration)
        local goal = {}
        goal.Position = closestHRP.Position
        local tween = TweenService:Create(npc.HumanoidRootPart, tweenInfo, goal)
        tween:Play()

        wait(chaseDuration)

        if p1 then
            local tweenInfoBack = TweenInfo.new(chaseDuration)
            local goalBack = {}
            goalBack.Position = p1.Position
            local tweenBack = TweenService:Create(npc.HumanoidRootPart, tweenInfoBack, goalBack)
            tweenBack:Play()
        else
            warn("Part 'p1' ")
        end
    end

    if npc.Humanoid.Health <= 0 then
        npc.Humanoid:MoveTo(npc.HumanoidRootPart.Position) 
        for _, player in pairs(plrs) do
            plrsHit[player] = false 
        end
        npc.Humanoid.Health = 0 
        break 
    end

end``` like this? @primal sedge
primal sedge
#

did it work?

polar fox
#

no

primal sedge
#

dont tween position tween CFrame

polar fox
#
local hrpOfNPC = npc:WaitForChild("HumanoidRootPart")

local plrsHit = {}
local maxDistance = 20

local p1 = workspace:FindFirstChild("p1")

local TweenService = game:GetService("TweenService")

npc.Humanoid.Touched:Connect(function(touch)
    local player = game.Players:GetPlayerFromCharacter(touch.Parent)
    if player and npc.Humanoid.Health > 0 and not plrsHit[player] then
        local playerCharacter = player.Character
        if playerCharacter and playerCharacter:FindFirstChild("Humanoid") and playerCharacter.Humanoid.Health > 0 then
            plrsHit[player] = true
            playerCharacter.Humanoid:TakeDamage(20)
            wait(1)
            plrsHit[player] = false
        end
    end
end)

while wait() do
    local plrs = game.Players:GetPlayers()
    local closestHRP

    for i, plr in pairs(plrs) do
        local playerCharacter = plr.Character
        if playerCharacter and playerCharacter:FindFirstChild("HumanoidRootPart") and playerCharacter.Humanoid.Health > 0 then
            local hrp = playerCharacter.HumanoidRootPart
            local distanceBetween = (hrpOfNPC.Position - hrp.Position).Magnitude

            if not closestHRP then
                closestHRP = hrp
            end

            if (hrpOfNPC.Position - closestHRP.Position).Magnitude > distanceBetween then
                closestHRP = hrp
            end
        end
    end

    if closestHRP and (hrpOfNPC.Position - closestHRP.Position).Magnitude <= maxDistance then
        local goal = CFrame.new(closestHRP.Position)
        local tweenInfo = TweenInfo.new(chaseDuration, Enum.EasingStyle.Linear)
        local tween = TweenService:Create(hrpOfNPC, tweenInfo, {CFrame = goal})
        tween:Play()

        wait(chaseDuration)

        if p1 then
            local goalBack = CFrame.new(p1.Position)
            local tweenBack = TweenService:Create(hrpOfNPC, tweenInfo, {CFrame = goalBack})
            tweenBack:Play()
        else
            warn("Part 'p1' not found!")
        end
    end

    if npc.Humanoid.Health <= 0 then
        npc.Humanoid:MoveTo(npc.HumanoidRootPart.Position) 
        for _, player in pairs(plrs) do
            plrsHit[player] = false 
        end
        npc.Humanoid.Health = 0 
        break 
    end

end```
like this? @primal sedge
vernal inletBOT
#

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

primal sedge
#

yea

vernal inletBOT
#

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

polar fox
#

now he does not move

slender flint
#

i think its has to do something about the movement

#

i havent looked at the code fully but if you already did i would make it stop then move back.

polar fox
slender flint
slender flint
#

maybe its because its still trying to follow you but its also trying to go back

polar fox
slender flint
polar fox
#

but why isnt the npc moving with the script

#

it just stands still but still does damage

slender flint
#

explain the moving\

#

is it following you at least?

polar fox
#

no it just stands still

#

but it does damage

slender flint
#

does the damage part come before or after the movement part

polar fox
#

the damage part is executed when the NPC touches another player

#

so movement is before damage

slender flint
#

hmm

#

wait so ur saying movement script comes before the damaging script?

polar fox
#

yea

#

In the script, the NPC is looking for the player, and he is moving to the player (movement script), and if the NPC is at the player, he is damaging the player (damaging script).

vernal inletBOT
#

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

slender flint
#

then it has to do something with your movement script

#

i would try to insert your 1st script that seemed to wrk

slender flint
#

correct

polar fox
#

yea its working but the npc isnt going back to the part

slender flint
#

make the npc stop

polar fox
#

okay i try it

slender flint
#

make the npc stop moving when it hits 3 seconds and make sure to put it before it goes back

polar fox
#

you mean like this? ```local npc = script.Parent
local hrpOfNPC = npc:WaitForChild("HumanoidRootPart")

local plrsHit = {}
local maxDistance = math.huge
local chaseDuration = 3

local p1 = workspace:FindFirstChild("p1")

npc.Humanoid.Touched:Connect(function(touch)
local player = game.Players:GetPlayerFromCharacter(touch.Parent)
if player and npc.Humanoid.Health > 0 and not plrsHit[player] then
local playerCharacter = player.Character
if playerCharacter and playerCharacter:FindFirstChild("Humanoid") and playerCharacter.Humanoid.Health > 0 then
plrsHit[player] = true
playerCharacter.Humanoid:TakeDamage(20)
wait(1)
plrsHit[player] = false
end
end
end)

while wait() do
local plrs = game.Players:GetPlayers()
local closestHRP

for i, plr in pairs(plrs) do
    local playerCharacter = plr.Character
    if playerCharacter and playerCharacter:FindFirstChild("HumanoidRootPart") and playerCharacter.Humanoid.Health > 0 then
        local hrp = playerCharacter.HumanoidRootPart
        local distanceBetween = (hrpOfNPC.Position - hrp.Position).Magnitude

        if not closestHRP then
            closestHRP = hrp
        end

        if (hrpOfNPC.Position - closestHRP.Position).Magnitude > distanceBetween then
            closestHRP = hrp
        end
    end
end

if closestHRP and (hrpOfNPC.Position - closestHRP.Position).Magnitude <= maxDistance then
    npc.Humanoid:MoveTo(closestHRP.Position)
    wait(chaseDuration)
    npc.Humanoid:MoveTo(npc.HumanoidRootPart.Position)
    if p1 then
        npc.Humanoid:MoveTo(p1.Position)
    else
        warn("Part 'p1' not found!")
    end
end

if npc.Humanoid.Health <= 0 then
    npc.Humanoid:MoveTo(npc.HumanoidRootPart.Position)
    for _, player in pairs(plrs) do
        plrsHit[player] = false
    end
    npc.Humanoid.Health = 0
    break
end

end```

#

wait i think i made it

#

i added a range

slender flint
#

nice

#

does it work

polar fox
#

yea thank you @slender flint

#

without you i would work the whole night 😂

slender flint
#

😂

#

youre welcome

polar fox
#

wait

#

i found a problemm

#

you can trigger the npc so he dont do his 3 seconds

#

@slender flint

slender flint
#

let me see

polar fox
#
local hrpOfNPC = npc:WaitForChild("HumanoidRootPart")

local plrsHit = {}
local detectionRange = 20 -- Adjust the range as desired
local chaseDuration = 3

local p1 = workspace:FindFirstChild("p1")

npc.Humanoid.Touched:Connect(function(touch)
    local player = game.Players:GetPlayerFromCharacter(touch.Parent)
    if player and npc.Humanoid.Health > 0 and not plrsHit[player] then
        local playerCharacter = player.Character
        if playerCharacter and playerCharacter:FindFirstChild("Humanoid") and playerCharacter.Humanoid.Health > 0 then
            plrsHit[player] = true
            playerCharacter.Humanoid:TakeDamage(20)
            wait(1)
            plrsHit[player] = false
        end
    end
end)

while wait() do
    local plrs = game.Players:GetPlayers()
    local closestHRP

    for i, plr in pairs(plrs) do
        local playerCharacter = plr.Character
        if playerCharacter and playerCharacter:FindFirstChild("HumanoidRootPart") and playerCharacter.Humanoid.Health > 0 then
            local hrp = playerCharacter.HumanoidRootPart
            local distanceBetween = (hrpOfNPC.Position - hrp.Position).Magnitude

            if distanceBetween <= detectionRange then -- Check if player is within the detection range
                if not closestHRP then
                    closestHRP = hrp
                end

                if (hrpOfNPC.Position - closestHRP.Position).Magnitude > distanceBetween then
                    closestHRP = hrp
                end
            end
        end
    end

    if closestHRP then
        npc.Humanoid:MoveTo(closestHRP.Position)
        wait(chaseDuration)
        npc.Humanoid:MoveTo(npc.HumanoidRootPart.Position)
        if p1 then
            npc.Humanoid:MoveTo(p1.Position)
        else
            warn("Part 'p1' not found!")
        end
    end

    if npc.Humanoid.Health <= 0 then
        npc.Humanoid:MoveTo(npc.HumanoidRootPart.Position)
        for _, player in pairs(plrs) do
            plrsHit[player] = false
        end
        npc.Humanoid.Health = 0
        break
    end
end``` this is script i use right now
slender flint
#

video doesnt load

polar fox
#

should i send it again?