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

XP on Hit/Kill NPC Not working? No errors?

So I've been trying to get my XP Script to function so if the player's bullet hits the NPC, you gain a certain number of XP, and once you kill that NPC, you gain a certain number as well, yet all my efforts on trying to get this solved haven't worked. Yet, there's no errors in the script, or when ran. So, if anyone knows how to fix this script or what to change, I'd love to the help! Thanks!

function onHealthChanged() 
	local zHuman =  script.Parent:findFirstChild( "Humanoid" )
	if zHuman ~= nil then 
		local tag = zHuman:findFirstChild( "creator" ) 
		if tag ~= nil then 
			if tag.Value ~= nil then 
				local stats = tag.Value:findFirstChild("XP") 
				if stats ~= nil then 
					if tag.Value.Character ~= nil then 
						stats.XP.Value = stats.XP.Value + 20 
					end
				end
			end 
		end 
	end
	
end 

function onDeath()
	local zHuman =  script.Parent:findFirstChild( "Humanoid" )
	if zHuman ~= nil then 
		local tag = zHuman:findFirstChild( "creator" ) 
		if tag ~= nil then 
			if tag.Value ~= nil then 
				local stats = tag.Value:findFirstChild("XP") 
				if stats ~= nil then 
					if tag.Value.Character ~= nil then 
						stats.XP.Value = stats.XP.Value + 200 
					end
				end
			end 
		end 
	end
	
end 
script.Parent.Humanoid.Died:connect(onDeath) 
script.Parent.Humanoid.HealthChanged:connect(onHealthChanged) 

7 comments

0

Is this script in the player's character?

0

Script is in the NPC's character.

0

A lot of thing are deprecated in this script. E.g 'findFirstChild" should be 'FindFirstChild'. and at the end, 'connect' should be 'Connect'

0

here's a fact: leaderstats are not located in the character with the Humanoid, that's why it isn't working

0

Okay, so where should I place the script? In ServerScriptService, Workspace, or?

0

inside of the player

0

but in order to make that work you have to edit the script quite a bit: for example, find when the bullet hits the NPC from a server script, use a remote event to fire that event to the client and then change the stats via the client

1 answer

Okay well when you're doing the if else statement on line 3 you are not calling the health you are just calling the property. If it were nill then it would not exist. And it always exist. To fix it just do "Zhuman.health"

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