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

How Can I use GetPlayerFromCharacter() to detect If the NPC summoned by a zombie staff?

Asked by 2 years ago

I wanted to script a weapon that summons zombies that follow the owner (The person who summoned them) and whoever comes near the zombies following the owner, It will target the said person and stop If the person died, I was told to use GetPlayerFromCharacter() to see If the zombies are touching there respective owner or a person that they will attack

More Information: The zombie Is being cloned from replicastorage The pathfinding appears to stop when being cloned from replicastorage

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
0
By the way instead of using while wait do use game:GetService("RunService").Heartbeat:Connect(function() to make it run every frame! MattVSNNL 620 — 2y
0
Okay! imnotaguest1121 362 — 2y

1 answer

Log in to vote
0
Answered by
AZDev 590 Moderation Voter
2 years ago

I'll try to give you the building blocks for what you are trying to do.

Whenever you use a tool in roblox, that tool and it's scripts has easy access to both the character and the player.

A tool can utilize both local scripts and server scripts.

First, on the Zombie side of this problem, you need a way to store the information that it needs to operate. You can pass this information along remotely using events(look that one up in the dev hub). You can also use different value objects like StringValue, IntValue, etc. However, I don't think it's wise to use value objects across server/client boundaries.

First, you don't necessarily need a reference to the player object. Only the character that summoned the zombie.

You can pass the character object along to the zombie when it's summoned.

When you are performing the target search you can use this information to exclude that character from the target pool.

GetPlayerFromCharacter becomes important if you want to verify that your target is indeed a player and not another NPC. Also, it would be wise to read up on this as well in the Developer Hub.

Ad

Answer this question