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

Why is my sword not tagging mobs?

Asked by 5 years ago
Edited 5 years ago

For some reason my code is working in studio but not in-game. I've been stumped on this for awhile now. The sword is supposed to tag a mob, then when the mob dies it awards the player with gold. But it doesnt work in-game

Heres the sword code; `

local tool = script.Parent
local handle = tool.Union
local cooldown = 0.45
local plr = game.Players.LocalPlayer

tool.Activated:Connect(function()
        if swinging then return end
        swinging = true
        wait(cooldown)
        if swinging then
        swinging = false
    end
end)

handle.Touched:Connect(function(hit)
        if swinging then
        swinging = false
        print("hit")
        local humanoid = hit.Parent:FindFirstChild('Enemy')
        if not humanoid then return end
        local tag = Instance.new('ObjectValue',humanoid)
        tag.Name = 'creator'
        tag.Value = plr
        humanoid:TakeDamage(12)
        wait(0.45)
        tag:Destroy()
     end
end)`

Heres the reward code: `

  local reward = 5

local npc = script.Parent

local human = npc.Enemy



human.Died:Connect(function()

local tag = script.Parent.Enemy:FindFirstChild('creator')

local killer = tag.Value

killer.Data.Stats.Gold.Value = killer.Data.Stats.Gold.Value + reward

end)
0
Heres the error it outputs http://prntscr.com/n04y5b UnitRook -5 — 5y
0
It's likely because "creator" doesn't exist. FindFirstChild() returns nil if the Instance does not exist in the directory FindFirstChild() is being called in. DeceptiveCaster 3761 — 5y
0
Yeah, I figured. Any idea on how to fix this though? UnitRook -5 — 5y
0
Relocate creator to be a child of Enemy if it already isn't a child of Enemy. DeceptiveCaster 3761 — 5y
0
The creator tag's parent is being set to the enemy. It's just not working in-game. UnitRook -5 — 5y

Answer this question