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

How do I make it so only the person that finished the quest gets money?

There is this script that is for a quest where you face a villain. But when you get the quest it will notify the server. But when the npc dies everyone who is doing the quest gets money how do I fix this.

local NPC = game.Workspace["NPC That Follows You"]
game.ReplicatedStorage.fightingquest.OnServerEvent:Connect(function(player)
	while true do
		wait()
		if NPC.Humanoid.Health <= 0 then 
			break
		end
	end
		player.leaderstats.Jenny.Value += 5
		--end
	
end)

1 answer

Instead of looping until the npc dies, log the damage done to the npc for each player and if they did enough damage, award the player. This way, you can easily check whether the player contributed or not and you can check when the npc dies without using a remote.

example script, not made to work.

local DAMAGE_VALUES = {}
local MINIMUM_DAMAGE_REQUIRED  = 1
local NPC = game.Workspace["NPC That Follows You"]
-- a way to detect when the npc dies
NPC.Humanoid.Died:Connect(function()
	for i,v in pairs(DAMAGE_VALUES) do
		-- if the player did enough damage
		if v.DAMAGE_DONE >= MINIMUM_DAMAGE_REQUIRED then
			local player = v.PLAYER_WHO_DAMAGED
			-- make sure player has correct quest
			if player.CURRENT_QUEST == "KILL THE NPC" then
				player.leaderstats.Jenny.Value += 5
			end
		end
	end
end)

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