Issues with tagging players in RPG game

So I’m trying to make tags for my RPG game for who did the damage to a NPC. The script I’m using should create a table of what is in the NPC, and if there isn’t a tag that says the player’s name then it should make one. However, it just ends up making a new tag each time it is hit rather than updating the already existing tag.

else if combo == 2 then
		SlashTrack2:Play()
	Damage = 5
	local Touched
	Touched = script.Parent.Handle.Touched:Connect(function(hit)
		if hit and hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= Player.Character then
			local Enemy = hit.Parent.Humanoid
			Enemy:TakeDamage(Damage)
				local itemlist = hit.Parent:GetChildren()
				local items = {}
				for i=1,#itemlist do
					if itemlist[i].Name == playername then
						local PlayerTag1 = itemlist[i]
						PlayerTag1.Value = PlayerTag1.Value + Damage
						Touched:Disconnect()
					else
						local DamageValue1 = Instance.new("NumberValue")
						DamageValue1.Name = playername
						DamageValue1.Value = Damage
						DamageValue1.Parent = hit.Parent
						Touched:Disconnect()
					return end
				end
			end
		end)
		wait(.5)
		Touched:Disconnect()
	end
	combo = 1
end
end)

instead of looping through the character you should just use:

if hit.Parent:FindFirstChild(playername) then -- Check if it finds the tag
       PlayerTag1 = hit.Parent:FindFirstChild(playername)
       PlayerTag1.Value += Damage
       Touched:Disconnect()
else -- Otherwise add the tag
       local DamageValue1 = Instance.new("NumberValue", hit.Parent)
	DamageValue1.Name = playername
	DamageValue1.Value = Damage
	Touched:Disconnect()
end

Also, why are you returning at the end, there should be no need, you can make a table, when something is hit, add the character/enemy to the table and so you can make it so that if it is hit again check if it is in the table. If it does then return, else deal damage adn add to table. At the end of the wait(0.5) then delete everything in the table/

Finally, make sure everything is defined correctly. For example. make sure that the playername is actually the player name etc.

Here are some useful links:
https://developer.roblox.com/en-us/articles/Table
https://developer.roblox.com/en-us/api-reference/lua-docs/table
https://developer.roblox.com/en-us/api-reference/function/Instance/FindFirstChild

Thank you a bunch! I honestly am not a noob at scripting but sometimes I just do not so smart things with my functions and dont think straight. I appreciate your answer a lot

1 Like