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

How can I modify the script so It follows other people and damaging them Instead of you?

Asked by 2 years ago

So What I mean Is that, I want to modify this script so when you summon a NPC It will protect you by following the other players, damaging them?

Oh and also a problem with the script Is that If being cloned lets say, replicatedstorage then, the pathfinding script stops working

script:

local NPC = script.Parent
local PathFindingService = game:GetService("PathfindingService")
local player

game.Players.PlayerAdded:Connect(function(client)
    player = client
end)

repeat wait() until player ~= nil

while wait() do
    local Path = PathFindingService:CreatePath()
    Path:ComputeAsync(NPC.Torso.Position, player.Character.PrimaryPart.Position)

    if Path.Status == Enum.PathStatus.Success then
        local Waypoints = Path:GetWaypoints()

        for i,v in pairs(Waypoints) do
            local Position = Instance.new("Part", workspace)
            Position.Position = v.Position
            Position.Anchored = true
            Position.CanCollide = false
            Position.Size = Vector3.new(1,1,1)

            NPC.Humanoid:MoveTo(Position.Position)
            NPC.Humanoid.MoveToFinished:Wait()
            Position:Destroy()
        end
    end
end

1 answer

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

There is a nifty function of the Players service called GetPlayerFromCharacter() that may catch your attention.

The function in question returns a player from the given instance only if it actually finds one. If it fails to find a player associated with the instance, it returns nil.

Why is this important? Well, you can use the function as a means of failsafe:

script.Parent.Touched:Connect(function(hit)
    local plr = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
    if plr then
        if hit.Parent:FindFirstChild("Humanoid") then
            hit.Parent.Humanoid:TakeDamage(10)
        end
    end
end)

The damaging portion of the script fails to activate if something touching it is not owned by a player.

Likewise, you can use this to create a zombie that only chases NPCs or the like by checking if the returned value is nil (not plr and plr == nil both work). In your case, you can use the function to check if the player the NPC hits is not you but rather a different player.

Also, cloning the script simply creates a new stub of code with a new PlayerAdded listener; the reason it doesn't run is because PlayerAdded does not fire more than once in solo testing. This is also the case if no player joins after you've cloned the script.

0
Okay, thanks for the Information! But as you stated on the PlayerAdded, what line of code should I replace It? imnotaguest1121 362 — 2y
0
It would be whatever portion of your code deals damage. DeceptiveCaster 3761 — 2y
Ad

Answer this question