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

My reward player on kill script isn't working?

I'm making a script that gives the player money when he kills a player and the player who died will loose money. But it doesn't work, I'm not sure why, I tried to debug it but I couldn't figure it out.

I am using a tagging system to find the player and who killed him.

Local script located in player starterpack (script that takes away the players money and gives the killer money when he dies):

local plr = game.Players.LocalPlayer
repeat wait(1) until plr:FindFirstChild("leaderstats")
local hum = plr.Character.Humanoid
local stats = plr:WaitForChild("leaderstats")
local team = stats:WaitForChild("Team")
local money = stats:WaitForChild("Money")

while wait(0.1) do
	hum.Died:connect(function()
		print("died") --Prints this
		
		local tag = hum:findFirstChild("creator") 
		if tag ~= nil then 
			if tag.Value ~= nil then 
				print("killed") --Prints this
				local Leaderstats = tag.Value:findFirstChild("leaderstats")
				if Leaderstats.Money.Value >= 100 then 
					print("Take") -- Doesn't print this. so I'm guessing the if statment isn't working. But I made sure to make the players money stat 100
					money.Value = money.Value - 100
					Leaderstats.Money.Value = Leaderstats.Money.Value + 100
				end
			end
		end
	end)
end

Tag script:

function tagHumanoid(humanoid, player)
	local creator_tag = Instance.new("ObjectValue")
	creator_tag.Value = player
	creator_tag.Name = "creator"
	creator_tag.Parent = humanoid
end

function untagHumanoid(humanoid)
	if humanoid ~= nil then
		local tag = humanoid:findFirstChild("creator")
		if tag ~= nil then
			tag.Parent = nil
		end
	end
end

--There more to this script but that doesn't matter because I know the taging system works

1 answer

✓ Accepted answer

On phone but, where u have the Variable "Leaderstats" I see you are trying to find the one who tagged the player. To do this, u need to on in the Players service.

local Leaderstats = game.Players:FindFirstChild(tag.Value).Leaderstats

Hope this helps bud! If it did, a upvote would be great!

6 comments

0

Ugh, I have the autocorrection. Sorry about that.

0

Hate*

0

Wow, thanks for trying to answer my question on your phone. I replaced that line of code and I get the error: "attempt to index a nil value" on that line.

0

nvm I fixed it thanks!

0

Np

0

How'd you fix the nil value error?

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