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

How would I reach the backpack of an enemy that is being punched?

Asked by 8 years ago

at line 3 I want to reach the backpack because there is a value named defe and I need that value so I can divide "d" and "Defense"

    if Part.Parent:FindFirstChild("Humanoid")~= nil then
            local attk = script.Parent.Parent.mLevel.Value
local Defense = Part.Parent.Parent.Parent.Backpack:WaitForChild("defe").Value
local d = attk/Defense*7+2
            local dmg = d/Defense
Part.Parent.Humanoid:TakeDamage(dmg)

1 answer

Log in to vote
0
Answered by
Im_Kritz 334 Moderation Voter
8 years ago

To reach the backpack you need to get the player, not the character. To define the player, I recommend you use :GetPlayerFromCharacter()

local player = game:GetService('Players'):GetPlayerFromCharacter(Part.Parent)

Full code:

    if Part.Parent:FindFirstChild("Humanoid")~= nil then
            local attk = script.Parent.Parent.mLevel.Value
local player = game:GetService('Players'):GetPlayerFromCharacter(Part.Parent)
local Defense = player.Backpack:WaitForChild("defe").Value
local d = attk/Defense*7+2
            local dmg = d/Defense
Part.Parent.Humanoid:TakeDamage(dmg)

Ad

Answer this question