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

How to Make Randomly Generated Monsters Take Damage From Tool/Weapon?

Asked by 5 years ago

I'm trying to figure out a way for my randomly generated monsters to take damage from clicking on them while a tool (gun) is equipped. Each monster has a script which watches its health, and if it drops below zero the monster is destroyed. This works fine (I can currently click on them without the tool and it works fine)

My problem is trying to get the tool to do the same thing. I've tried bindable functions on the monsters (so when I click on a monster, its damage function is run as each monster is different with health, etc)), but the mouse click event can only be used in local scripts, and the bindable event is said to be only usable in regular scripts. So I'm not sure what to do.

Code in my weapon (tool):

   local damageRating = 1

   tool.Equipped:Connect(function(Mouse)
    print("I've equipped the weapon")
    toolEquiped = true
    Mouse.Button1Down:Connect(function()
           print("Clicking the mouse")
        weaponHit(Mouse)
    end)
   end)

   function weaponHit(Mouse)
    print("Pow!")
       if toolEquiped then
           local target = Mouse.Target
           if target.Name == "Monster" then
            target.Hurt:Invoke(damageRating)
           end
       end
   end

Code in my monster (I've added a Hurt "BindableFunction" to the monster):

   character.Hurt.OnInvoke = function(damage)
    health = health - damage
    if health <= 0 then
        script.Parent:Destroy()
    end
   end

I get the error: "Hurt is not a valid member of Part"

So, I feel either Hurt is not accessible with the local Script, or I've done this wrong. Any tips to fill in the missing pieces would be amazing, thanks!

Answer this question