NPC Cash reward doesn't work

Hello, somewhat of a beginner scripter here, I am trying to make a really simple game just to have some fun, but I am in a road block right now. I have a weapon I scripted myself that gives a “tag” to the target humanoid, and the script inside the humanoid detects the tag and is supposed to give the cash reward, but that is not the case and I am confused on why, because it does not even detect the tag. The script works just fine with the default sword, and my weapon basically does the same thing unless I am just being stupid.

This is the reward script:

local hum = script.Parent
if hum then
	print("got hum")
end
local npc = hum.Parent

hum.Died:Connect(function()
	local tag = hum:FindFirstChild("creator")
	if tag then
		print("got tag")
		if tag ~= nil then
			if tag.Value ~= nil then
				local leaderstats = tag.Value:FindFirstChild("leaderstats")
				if leaderstats ~= nil then
					leaderstats.Cash.Value = leaderstats.Cash.Value + 10
				end
			end
		end
	end
end)

Also here are two videos showing both cases.


2 Likes

can you please drop the tag giver code you made?

1 Like

Here.

local function damage(hit)
	local targetHumanoid = hit.Parent:FindFirstChild("Humanoid")
	if hit.Parent:FindFirstChild("Humanoid") and swinging then
		if hit.Parent.Humanoid:IsA("Humanoid") and debounce == false then
			debounce = true
			hit.Parent.Humanoid:TakeDamage(50)
			if targetHumanoid.Health > 0 then
				if targetHumanoid:FindFirstChild("creator") or targetHumanoid:FindFirstChildWhichIsA("StringValue") then
					targetHumanoid:FindFirstChildWhichIsA("StringValue"):Destroy()
				end
				
				task.wait()
				
				local Tag =  Instance.new("StringValue")
				Tag.Name = "creator"
				Tag.Value = player.Name
				Tag.Parent = targetHumanoid
			end
			sound:Play()
			task.wait(1)
			debounce = false
		end
	end
end

tool.Handle.Touched:Connect(damage)

Is the print statement “got tag” even printing? Also you could try debugging further more with print statements.

when killed with the stick, it doesn’t print “got tag”, it however prints “got tag” if you kill it with the classic sword, which is why I am really confused

May I see the script for your stick tool?

it’s above when I replied to amandasillv

Did you make any changes to this script that differ from the classic sword script?

i didn’t really use the classic sword script to make my own, I only inspected it right after when I was trying to fix the problem, but it follows the same premise where it gives the “creator” tag for the reward, i put videos in the og post

Odd, I don’t see any issues. Try making it “WaitForChild” instead of FindFirstChild.

i am sure i already tried that but to no avail, oh well, i will try to find a workaround, thank you for trying to help me

Hm, try adding a print statement after ‘hum.Died’, see if it’s even called.

it is called, but only when killed with the default sword lol

Well the issue relies on your stick script then, it doesn’t have to do with the tag.

honestly, most likely, ill look into it later

Alrighty, good luck with that!

1 Like

Is it possible that the tool script is a local script and not a server script?
(If you don’t know the difference between these two i’d recommend reading about them in the docs)
If yes then that’s probably the first issue you made here, cause i’ve tried the tool script that you sent with local script (it did the same thing as your vid) and then i made a server script, modified the original tool script and then it errored out because of the way that you made the creator tag in the stick work which is your second issue, it works differently than in the sword script which works like this:

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

The error you made is that instead of creating an ObjectValue you made a StringValue which are quite different (i recommend that you read about them in the docs to learn what the differences are) and then you put in only the name which should be the player object (in future we will copy the sword script which works correctly).

local Tag =  Instance.new("StringValue") --Should be: local Tag =  Instance.new("ObjectValue")
Tag.Name = "creator"
Tag.Value = player.Name --Should be: Tag.Value = player
Tag.Parent = targetHumanoid

The sword script also uses the Debris service which is very useful and i would recommend reading about it if you don’t know(in short it basically deletes an instance in a given time), and the Players service which i’m pretty sure you’re already familiar with, and a couple of other functions that work with tools which we’ll use like:
tool.Activated
tool.Equipped

So with these changes you should have code similar to this which worked, also i commented out certain variable that weren’t in the script you sent, also add your animation code in the Activated function and as a disclaimer i’m not very experienced with tools so if anybody that’s reading this found any issues/errors i made, please point them out thx (also the humanoid script stays unchanged):

tool = script.Parent

Players = game:GetService("Players")
Debris = game:GetService("Debris")

swinging = false
debounce = false

--These change when the player equips the weapon
Character = nil 
Player = nil
Humanoid = nil

--Since you didn't send the entire tool script i use this swinging system
local function Activated()
	if swinging then
		return --we wont do anything because the player is already swinging
	else
		--Add your animation code here
		
		swinging = true
		wait(0.5) --you could also use the animation time
		swinging = false
	end
end

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

local function damage(hit)
	local targetHumanoid = hit.Parent:FindFirstChild("Humanoid")
	if hit.Parent:FindFirstChild("Humanoid") and swinging then
		if hit.Parent.Humanoid:IsA("Humanoid") and debounce == false then
			debounce = true
			hit.Parent.Humanoid:TakeDamage(50)
			UntagHumanoid(targetHumanoid)--Destroys all other tags so we dont have multiple tags on the same humanoid.
			TagHumanoid(targetHumanoid, Player)
			--sound:Play()
			task.wait(1) -- you could also probably just use the animation time in here aswell
			debounce = false
		end
	end
end

function Equipped()
	Character = tool.Parent
	Player = Players:GetPlayerFromCharacter(Character)
	Humanoid = Character:FindFirstChildOfClass("Humanoid")
end

--Activated is basically when the player presses the mouse button or taps on the mobile
tool.Activated:Connect(Activated)

tool.Equipped:Connect(Equipped)

tool.Handle.Touched:Connect(damage)
1 Like

Oh my god, thank you so much for this. It finally worked. I didn’t realize that the classic sword script wasn’t in a local script. My stick script was in a local script, so that is probably part of the reason as to why it didn’t work. I also modified it based on what you posted. Thank you again!

1 Like

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