Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why won't my weapon tag the player?

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

✓ Accepted answer

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.

3 comments

0

Thank you! Sorry for the poor indentation, I'll make sure to take care of that in the future.

0

No problem.

0

Also, something I'm only just now noticing.. It doesn't seem like the while loop is even necessary.

This discussion is preserved as it was. New answers, comments, and votes are closed.