how to make it find enemy backpack this is my script
local char = Player.Character
local hum = char.Humanoid
local ignorelist = {}
local Hitbox = script.Hitbox:Clone()
Hitbox.Parent = game.Workspace
Hitbox.CFrame = char.PrimaryPart.CFrame * CFrame.new(0,0,Hitbox.Size.Z/2 * -1)
Debris:AddItem(Hitbox,.3)
local Connection
Connection = Hitbox.Touched:Connect(function(hitpart)
if not hitpart:IsDescendantOf(char) then
if hitpart.Parent:FindFirstChild("Humanoid") then
local enemy = hitpart.Parent
1 Like
You can sue Players | Roblox Creator Documentation to do it, the character is equal the character model which is usually hitpart.Parent if a humanoid has already been found, keep in mind this also detects NPC’s so another if statement is required. Then once the player instance is found you can do player.Backpack
if you want more accuracy you can use hitpart:FindFirstAncestorWhichIsA(“Model”) as well to get the character model if it hits an accessory somehow.
how should i change to enemy backpack?
local enemy = hitpart.Parent
local enemyPlr = game:GetService("Players"):GetPlayerFromCharacter(enemy)
local enemyBackpack = enemyPlr.Backpack -- Enemy backpack
I think this should do it?
Is the enemy a player? or an npc?
is a enemy i hit him and the output print this
I have high confidence that the hitpart.Parent is equal to an accessory which tend to mess up these detections or npc.
debug with prints and use an if statement to filter out those scenarios.
local enemy = hitpart:FindFirstAncestorWhichIsA("Model")
local enemyPlr = game:GetService("Players"):GetPlayerFromCharacter(enemy)
if enemyPlr then
local enemyBackpack = enemyPlr.Backpack -- Enemy backpack
else
warn("Enemy player not found! Probably an enemy NPC with no backpack or unrelated part, the hit item is: ",hitpart,enemy)
end