Need a bit of assistance with rewarding multiple players after killing an NPC

What im trying to do is reward multiple players after killing an NPC

The issue is when multiple people kill the NPC players get rewards multiple times

I’ve been looking for a solution on the developer hub but i haven’t found anything about giving multiple players a reward after killing an NPC (They usually only talk about last hitting which is not what im trying to do)

I apologize if this doesn’t make any sense to some people I wouldn’t have posted this if i could’ve found something that would have led me in the right direction (the script below is the NPC death script) help would be appreciated!

local humanoid = script.Parent.Humanoid
local anims = script.Parent.anims
debounnce = false
local maxlevel = game.ReplicatedStorage:WaitForChild("Stats").maxlevel.Value
humanoid.Died:Connect(function() -- Humanoid died
	if debounnce == false then
	debounnce = true
	for _, I in pairs(script.Parent:GetDescendants()) do --change collision id on death
		if I:IsA("BasePart") then
			I.CollisionGroupId = "1"
		end
	end
	
	local DeathAnim = humanoid:LoadAnimation(anims.death)
	DeathAnim:Play()
	local boolvalue = Instance.new("BoolValue")
	boolvalue.Parent = script.Parent
	boolvalue.Name = "NPC"
			
	for _, I in pairs(script.Parent:GetDescendants()) do
		if I:IsA("ObjectValue") and I.Name == "WhoHit" then
				
				
			
			local player = I.Value
			local rewardplrvalue = Instance.new("ObjectValue")
			rewardplrvalue.Parent = script.Parent
			rewardplrvalue.Name = "rewardedplayer"
			
				if I.Value == rewardplrvalue.Value then
					-- ALREADY GOT
				else
					
				rewardplrvalue.Value = I.Value
				
				if player.PlayerGui:FindFirstChild("alreadygot") == nil  then
					
					
					if player.PlayerGui:FindFirstChild("banditquest") ~= nil then
						local banditquest = player.PlayerGui:WaitForChild("banditquest")
						
						if banditquest.Quest.number.min.Value >= 4 then
							print("DONE")
							banditquest.Quest.number.min.Value = 5
							player.Character.hasquest.Value = false
							local sounds = game.ReplicatedStorage:WaitForChild("Sounds")
							local ServerScriptService = game:GetService("ServerScriptService")
							local DataStore2 = require(ServerScriptService.GameScripts.DataStore2)
							local ServerStorage = game:GetService("ServerStorage")
							local Amount = 70
							


							local hudgui = player.PlayerGui:WaitForChild("HUD")
							hudgui:WaitForChild("Health").gainxp.Text = "+"..Amount

							hudgui:WaitForChild("Health").gainyuls.TextTransparency = 0
							hudgui:WaitForChild("Health").gainxp.TextTransparency = 0


							local xpDifference = ServerStorage["Stats"..player.Name].MAXXP.Value - ServerStorage["Stats"..player.Name].XP.Value

							local leftOverXP = Amount - xpDifference
							for i = 1,Amount do
								ServerStorage["Stats"..player.Name].XP.Value = ServerStorage["Stats"..player.Name].XP.Value + 1

								if ServerStorage["Stats"..player.Name].XP.Value >= ServerStorage["Stats"..player.Name].MAXXP.Value then
								ServerStorage["Stats"..player.Name].XP.Value =  ServerStorage["Stats"..player.Name].XP.Value - ServerStorage["Stats"..player.Name].MAXXP.Value
									
									if ServerStorage["Stats"..player.Name].Level.Value < maxlevel then
										
									local lvlupsound = sounds:WaitForChild("levelup"):Clone()
									lvlupsound.Parent = player.Character.HumanoidRootPart
									lvlupsound:Play()
									local addskillpoints = DataStore2("StatPoints",player)
									addskillpoints:Increment(3)
									local lvlupeffect = game.ReplicatedStorage:WaitForChild("Effects").LevelUp:Clone()
									lvlupeffect.Parent = workspace
									lvlupeffect.CFrame = player.Character.HumanoidRootPart.CFrame
									lvlupeffect.Attachment.Flare:Emit(1)
									lvlupeffect.Attachment.Sparkles:Emit(1)
									lvlupeffect.Attachment.Wave:Emit(1)
									local lvlupweld = Instance.new("WeldConstraint",lvlupeffect)
									lvlupweld.Part0 = lvlupeffect
									lvlupweld.Part1 = player.Character.HumanoidRootPart
									game:GetService("Debris"):AddItem(lvlupeffect,1.5)
									game:GetService("Debris"):AddItem(lvlupsound,1.656)
									
									ServerStorage["Stats"..player.Name].Level.Value = ServerStorage["Stats"..player.Name].Level.Value + 1
									ServerStorage["Stats"..player.Name].MAXXP.Value = ServerStorage["Stats"..player.Name].MAXXP.Value + 80
										
									end
								end
							end

							wait(1.5)





							local updateXP = DataStore2("XP",player)
							updateXP:Set(ServerStorage["Stats"..player.Name].XP.Value)

							local updatelevel = DataStore2("Level",player)
							updatelevel:Set(ServerStorage["Stats"..player.Name].Level.Value)

							local updateMAXXP = DataStore2("MAXXP",player)
							updateMAXXP:Set(ServerStorage["Stats"..player.Name].MAXXP.Value)





							for i = 1,10 do
								wait()
								hudgui:WaitForChild("Health").gainxp.TextTransparency = hudgui:WaitForChild("Health").gainxp.TextTransparency + 0.1
							end

						
							hudgui:WaitForChild("Health").gainxp.Text = ""
							
							wait(1)
							
							banditquest:Destroy()
							
						else
							banditquest.Quest.number.min.Value = banditquest.Quest.number.min.Value + 1
						end
					end
					
			
					
			local sounds = game.ReplicatedStorage:WaitForChild("Sounds")
			local ServerScriptService = game:GetService("ServerScriptService")
			local DataStore2 = require(ServerScriptService.GameScripts.DataStore2)
			local ServerStorage = game:GetService("ServerStorage")
			local Amount = 10
			local randommoneygain = math.random(5,10)
			local moneh = DataStore2("Yuls",player)
			moneh:Increment(randommoneygain)
			
			local hudgui = player.PlayerGui:WaitForChild("HUD")
			hudgui:WaitForChild("Health").gainyuls.Text = "+"..randommoneygain
			hudgui:WaitForChild("Health").gainxp.Text = "+"..Amount
			
			hudgui:WaitForChild("Health").gainyuls.TextTransparency = 0
			hudgui:WaitForChild("Health").gainxp.TextTransparency = 0
			
			local purchasesound = sounds:WaitForChild("buy"):Clone()
			purchasesound.Parent = player.PlayerGui
			purchasesound:Play()
			game:GetService("Debris"):AddItem(purchasesound,0.29)
			
					
			local xpDifference = ServerStorage["Stats"..player.Name].MAXXP.Value - ServerStorage["Stats"..player.Name].XP.Value

			local leftOverXP = Amount - xpDifference
			for i = 1,Amount do
				ServerStorage["Stats"..player.Name].XP.Value = ServerStorage["Stats"..player.Name].XP.Value + 1

				if ServerStorage["Stats"..player.Name].XP.Value >= ServerStorage["Stats"..player.Name].MAXXP.Value then
					ServerStorage["Stats"..player.Name].XP.Value =  ServerStorage["Stats"..player.Name].XP.Value - ServerStorage["Stats"..player.Name].MAXXP.Value
						
					if ServerStorage["Stats"..player.Name].Level.Value < maxlevel then
								
					local lvlupsound = sounds:WaitForChild("levelup"):Clone()
					lvlupsound.Parent = player.Character.HumanoidRootPart
					lvlupsound:Play()
					local addskillpoints = DataStore2("StatPoints",player)
					addskillpoints:Increment(3)
					local lvlupeffect = game.ReplicatedStorage:WaitForChild("Effects").LevelUp:Clone()
					lvlupeffect.Parent = workspace
					lvlupeffect.CFrame = player.Character.HumanoidRootPart.CFrame
					lvlupeffect.Attachment.Flare:Emit(1)
					lvlupeffect.Attachment.Sparkles:Emit(1)
					lvlupeffect.Attachment.Wave:Emit(1)
					local lvlupweld = Instance.new("WeldConstraint",lvlupeffect)
					lvlupweld.Part0 = lvlupeffect
					lvlupweld.Part1 = player.Character.HumanoidRootPart
					game:GetService("Debris"):AddItem(lvlupeffect,1.5)
					game:GetService("Debris"):AddItem(lvlupsound,1.656)
					ServerStorage["Stats"..player.Name].Level.Value = ServerStorage["Stats"..player.Name].Level.Value + 1
					ServerStorage["Stats"..player.Name].MAXXP.Value = ServerStorage["Stats"..player.Name].MAXXP.Value + 80
								
					end
				end
			end

			wait(1.5)
			
			
			
			
			
			local updateXP = DataStore2("XP",player)
			updateXP:Set(ServerStorage["Stats"..player.Name].XP.Value)

			local updatelevel = DataStore2("Level",player)
			updatelevel:Set(ServerStorage["Stats"..player.Name].Level.Value)

			local updateMAXXP = DataStore2("MAXXP",player)
			updateMAXXP:Set(ServerStorage["Stats"..player.Name].MAXXP.Value)
			
			
			
			
			
			for i = 1,10 do
				wait()
				hudgui:WaitForChild("Health").gainyuls.TextTransparency = hudgui:WaitForChild("Health").gainyuls.TextTransparency + 0.1
				hudgui:WaitForChild("Health").gainxp.TextTransparency = hudgui:WaitForChild("Health").gainxp.TextTransparency + 0.1
			end
			
			hudgui:WaitForChild("Health").gainyuls.Text = ""
			hudgui:WaitForChild("Health").gainxp.Text = ""
			
			
			
			
				end
			end
		end
	end
end
	
	
	
		
	
end)

How are you rewarding them? Is it based on the amount of damage one does, or splitting it equally between the people who hit it?

Count the player hits in a table, then later reward them whatever you want.

Im rewarding everyone the same amount the problem is when multiple people kill the NPC they get rewarded multiple times (around like 2-3)

How would i go about doing that? do you have an example that could point me in the right direction

table.insert() is the way to go

1 Like