Npc only gives xp when killed by certain weapons (not asking to make another script and structure it Im asking to fix a bug feature in a certain way!)

For some reason when i kill an npc with certain tools it doesn’t work. How can I revise this script so that no matter what type of damage it is as long as the humanoid health reaches 0 you get the reward. (keep it a loop so its every time humanoid health reaches 0) but (its kinda for a different topic but it would be much easier to kill two birds with one stone so I don’t have to go re-organizing again to do this) I also want it so that the reward goes to everyone who killed it during the lifetime then once the npc respawns the data of who attacked the npc during its lifetime gets reset (make sure the things I am talking about go in order so when the data of who attacked the npc gets reset AFTER they receive the reward. I tried but I am not the best at structuring scripts. Here is the script needed to be revised. Just in case for the other thing I asked I will include the respawn script too. Also tell me which script I have to change the script with whether its Reward or Respawn or both (if so tell me which one is which) Please state the script and location so I just gotta copy and paste. (Not an example : Saying I need to use GetBlah server or smth (idk)) Anything helps!

Reward script (keep scrolling for respawn script):

local Humanoid = script.Parent.Humanoid
function PwntX_X() 
local tag = Humanoid:findFirstChild("creator") 
	if tag ~= nil then 
		if tag.Value ~= nil then 
local Data = tag.Value:findFirstChild("Data") 
			if Data ~= nil then 
Data.Gold.Value = Data.Gold.Value + 50
Data.Exp.Value = Data.Exp.Value + 100
wait(0.1) 
script:remove()
			end 
		end 
	end 
end 
Humanoid.Died:connect(PwntX_X) 

Respawn script:

Dummy = script.Parent:Clone()
Enemy = script.Parent:WaitForChild("Humanoid")
Hum = script.Parent
Enemy.Died:connect(function()
	wait(4)
	Dummy:Clone().Parent = script.Parent.Parent
	script.Parent:Destroy()
end)
7 Likes

With the title I am just adding that because some people originally thought I wanted them to make a whole different script. I just want them to fix the bug stated in the title and make it in a way where it gives the reward to everybody who attacked in the enemies lifetime. I entirely know what this script is. The forum exists to ask about problems with scripting and I am having a bug. Not asking to make a whole system. Just wanted to make that clear!

5 Likes

This stuff is really urgent because this is a huge roadblock in the process of adding to the xp system so like I said before anything helps!

5 Likes

Check your weapon and make sure it’s adding the creator tag. Also find first child is FindFirstChild not findFirstChild.

3 Likes

This tag function is found in most Roblox gear so you can take a look at them for some examples.

Now for the weapon part

-- In the main script where you deal damage, it should contain the following functions (Not local script)
function TagHumanoid(humanoid, player)
	local Creator_Tag = Instance.new("ObjectValue")
	Creator_Tag.Name = "creator"
	Creator_Tag.Value = player
	Debris:AddItem(Creator_Tag, 2)
	Creator_Tag.Parent = humanoid
end
-- [1] This  TagHumanoid function will create an ObjectValue (Tag) named "creator", with the player (Username)
-- [2] as its value. This tag has a duration of 2 seconds (Debris service) 
-- [3] Once you tag a humanoid, that humanoid will be the tag's parent

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

– [1] Loops through the humanoid’s children, if it finds an ObjectValue (Tag) named “creator” then
– [2] it will destroy the tag. This function is used to clear up the previous/already exisiting tags
– [3] within the humanoid, which prevents multiple players from getting the reward, as only the
– [4] player who dealt the final hit would get it

Finally, how to tag a humanoid with the attacker’s username
(TagHumanoid(humanoid, Player)) ← humanoid is the target, which will be where the tag is parented to

Part.Touched:connect(function(Hit)
	if Hit and Hit.Parent then
			local Character = Hit.Parent
			local Humanoid = Character:FindFirstChildOfClass("Humanoid")
			-- NOTE: Sometimes if you don't use FindFirstChildOfClass, it won't deal damage or tag the humanoid if it isn't named "Humanoid"
			UntagHumanoid(humanoid) -- Removes existing tags, if found any
			TagHumanoid(humanoid, Player) -- Tags the target's humanoid with an ObjectValue
			 -- This tag has the attacker's username as its value
			Humanoid:TakeDamage(Damage) -- Now deals damage
		end
	end
end)

Oh wait, there’s more?
How to get the attacker’s username (TagHumanoid(humanoid, Player) ← Send the player’s username

function Equipped()
	Character = Tool.Parent
	Player = Players:GetPlayerFromCharacter(Character)
end

For instance, Roblox’s Linked Sword:

2 Likes

I can show more examples (Beside gears, like a gun or a part, turret, etc) or assist you further with the script if you need help
And for anybody in the future that found this helpful but still confused, send me a message and I’ll explain in details about this system. I used to have the same problem 3 years ago when I made my step into Roblox Studio lol

3 Likes

ima try it rq thx! I will give check if it works!

1 Like

@ThunderDaNub just a few questions. Where do you put the last 3 scripts. I have an idea but I just want to make sure I am doing things write. Also if they are scripts local scripts or smth else. (Ex: the first script is a local script in the npc dummy.) Just asking to give that info for the last 3. Thanks in advance!

1 Like

Also when I say last 3 I am not including the roblox’s linked sword as an example (u prob know that but just sayin)

1 Like

Sorry for the late reply
You need put the TagHumanoid & UntagHumanoid functions somewhere in the script that deals damage
For the next one, it’s just an example so you know how to use it
I think you should show me a part of your script and I’ll make it work right away

2 Likes
script.Parent.blade.Touched:connect(function(p)
  if script.Parent.CanDamage.Value == true then 
   p.Parent.Humanoid:TakeDamage(35) 
   script.Parent.CanDamage.Value = false 
  end 
 end) 

Btw can you make it so that I have somewhat of an idea of where to put it in the future because some abilities / tools work in other ways where the script is organized in a different fashion and I need a way to figure that out and rn I have no clue :P.

1 Like

Alright try this

local Players = game:GetService("Players")
local Debris = game:GetService("Debris")
local Tool = script.Parent

function TagHumanoid(humanoid, player)
	local Creator_Tag = Instance.new("ObjectValue")
	Creator_Tag.Name = "creator"
	Creator_Tag.Value = player
	Debris:AddItem(Creator_Tag, 2)
	Creator_Tag.Parent = humanoid
end

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

Tool.blade.Touched:connect(function(p)
	if p and p.Parent and p.Parent:FindFirstChildOfClass("Humanoid") then
		local Humanoid = p.Parent:FindFirstChildOfClass("Humanoid")

		if Tool.CanDamage.Value == true then
		UntagHumanoid(Humanoid)
		TagHumanoid(Humanoid, Player) 
		Humanoid:TakeDamage(35) 
		Tool.CanDamage.Value = false
		end
	end 
end)

function Equipped()
	Character = Tool.Parent
	Player = Players:GetPlayerFromCharacter(Character)
end
Tool.Equipped:connect(Equipped)

You can also use module script if you wanna handle multiple weapons instead of having hundreds of lines of codes

1 Like

A few questions. One where would the module script be if I used a module script. Another questions how would I change it for different weapons (if u could add to the script to make a template so its all I gotta due is keep copy and pasting a part to make the script include more weapons (also make sure that when you do this I can change the takedamage value for different weapons)) sorry if I am asking too much but I am not really used to that function (yet!)

You should learn more about module scripts in previous posts, I’ve already provided you everything you need for that (You can even reuse the same thing)

1 Like

Alright! Thanks for everything

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.