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

Why won't my weapon tag the player?

Asked by 7 years ago
Edited 7 years ago

I'm trying to make a weapon that once it kills a player it will give it points. I have a script that will do that but it only works on linked swords that tag the player. I tried putting the tagging part of that script into my own but it ends up crashing the game and saying "attempt to call global 'tagHumanoid' (a nil value), line 10" I'm not sure what is happening...

local script

wait(0.01)
local CanDamage = true
local plr = game.Players.LocalPlayer
while true do
    wait(0.01)
    script.Parent.Touched:connect(function(hum) 
    if hum and hum.Parent:FindFirstChild("Humanoid") then 
            if CanDamage == true then
                tagHumanoid(hum, plr)
                CanDamage = false
                hum.Parent.Humanoid:TakeDamage(10)
                plr.swordlvl.Value = plr.swordlvl.Value + 2
                wait(0.8)
                untagHumanoid(hum)
                CanDamage = true
            end
        end

end)

end

function tagHumanoid(humanoid, player)
    local creator_tag = Instance.new("ObjectValue")
    creator_tag.Value = player
    creator_tag.Name = "creator"
    creator_tag.Parent = humanoid
end

function untagHumanoid(humanoid)
    if humanoid ~= nil then
        local tag = humanoid:findFirstChild("creator")
        if tag ~= nil then
            tag.Parent = nil
        end
    end
end


1 answer

Log in to vote
0
Answered by
Pyrondon 2089 Game Jam Winner Moderation Voter Community Moderator
7 years ago

The reason you're getting this error is because you define the functions tagHumanoid and untagHumanoid after the loop; so, while the loop is running, those functions do not exist. This can be easily remedied:

wait(0.01)
local CanDamage = true
local plr = game.Players.LocalPlayer

local function tagHumanoid(humanoid, player) --// Usage of local functions is recommended
    local creator_tag = Instance.new("ObjectValue")
    creator_tag.Value = player
    creator_tag.Name = "creator"
    creator_tag.Parent = humanoid
end

local function untagHumanoid(humanoid)
    if humanoid ~= nil then
        local tag = humanoid:FindFirstChild("creator")
        if tag ~= nil then
            tag.Parent = nil
        end
    end
end

while true do
    wait(0.01)
    script.Parent.Touched:connect(function(hum) -- Changed "part" to "hum"
        -- local hum = part.Parent:FindFirstChild("Humanoid") Delete this
        if hum and hum.Parent:FindFirstChild("Humanoid") then -- "hum" is the Player who touched the part/Sword.
            if CanDamage == true then
                tagHumanoid(hum, plr)
                CanDamage = false
                hum.Parent.Humanoid:TakeDamage(10) -- I forgot to add the "Parent.Humanoid" part after "hum"
                plr.swordlvl.Value = plr.swordlvl.Value + 2
                wait(0.8)
                untagHumanoid(hum)
                CanDamage = true
            end
        end
    end)
end

Hope this helped!

PS: Your indentation was a bit off, so I went ahead and fixed it for you. In the future, it's a good idea to always properly indent your code so it's easier to read.

0
Thank you! Sorry for the poor indentation, I'll make sure to take care of that in the future. roblox99456789 104 — 7y
0
No problem. Pyrondon 2089 — 7y
0
Also, something I'm only just now noticing.. It doesn't seem like the while loop is even necessary. Pyrondon 2089 — 7y
Ad

Answer this question