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

Creator tag on custom weapon not tagging NPC?

I have been trying to make a tag that the server can read so it knows who killed who. I put my tag and untag functions into the remote event script so that when damage is dealt, the tag is also made. When the NPC dies, however, the console always returns the tag as nil. What am I doing wrong? I am desperate for help, I've tried figuring this out for days lol.

local debris = game:GetService("Debris")

local function tagHumanoid(hum, player)
	local tag = Instance.new("ObjectValue", hum)
	tag.Name = "creator"
	tag.Value = player
	debris:AddItem(tag, .5)
end

local function untagHumanoid(hum)
	for i, v in pairs(hum:GetChildren()) do
		if v:IsA("ObjectValue") and v.Name == "creator" then
			v:Destroy()
		end
	end
end

script.Parent.DealDamage.OnServerEvent:Connect(function(player, Target, Damage)
	untagHumanoid(Target)
	tagHumanoid(Target, player)
	Target.Humanoid:TakeDamage(Damage)
end)

This script is what tags, untags, and deals damage. I am almost certain this is where the tags need to be.

local hum = script.Parent.Humanoid

hum.Died:Connect(function()
	local tag = hum:FindFirstChild("creator")
	if tag then 
		local player = tag.Value
		local Cash = player.leaderstats.Cash
		Cash.Value = Cash.Value + 10
	end
end)

This is the script inside the NPC. It should find my creator tag but it always returns nil if I put a print statement under FindFirstChild.

1 answer

✓ Accepted answer

Target is the NPC Model. In the tagHumanoid function, you sent Target as the hum parameter. Therefore, your creator tags are being parented to the NPC Model instead of its Humanoid.

Easy fix would be to send Target.Humanoid. You could also just change the tagHumanoid function to work with the NPC Model.

2 comments

0

So I can change Target to Target.Humanoid so it will parent to the humanoid rather than the model? I'm new to Lua, sorry.

0

That did the trick. Can't believe I never tried Target.Humanoid, I didn't know there was a difference. Thank you!!

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