Trying to get the use of a tool to add to leaderstats currency

I have been trying for the longest time to get this glove to work with the currency system I’ve implemented. With the glove for every time you hit a dummy it adds to a leaderstats currency. I can only get it to add to myself (shown by my username in the code) and not all players individually. Any help would be appreciated.

function hitDollars()
	game.Players.LarvusIV.leaderstats.smackDollars.Value = game.Players.LarvusIV.leaderstats.smackDollars.Value + 5
end
function onTouched(part)
	if part:IsDescendantOf(Tool.Parent) then return end
	if not HitAble then return end
	
	if part.Parent and part.Parent:FindFirstChild("Humanoid") then
		local human = part.Parent.Humanoid
		hitDollars()
1 Like

To get the player from a touched event you would do something like this and put it after your check for the humanoid

local Player = Players:GetPlayerFromCharacter(part.Parent)

And then to pass it to the hitDollars function you would do hitDollars(Player)
And in your hitDollars function this

function hitDollars(Player)
	Player.leaderstats.smackDollars.Value = Player.leaderstats.smackDollars.Value + 5
end
1 Like

The only issue I’m facing is that the function cannot work when in the onTouched function, if not then there is another issue that I am not aware of.

function onTouched(part)
	if part:IsDescendantOf(Tool.Parent) then return end
	if not HitAble then return end
	
	if part.Parent and part.Parent:FindFirstChild("Humanoid") then
		local human = part.Parent.Humanoid
		local Player = game.Players:GetPlayerFromCharacter(part.Parent)

		function hitDollars(Player)
			Player.leaderstats.smackDollars.Value = Player.leaderstats.smackDollars.Value + 5
		end
		
		hitDollars(Player)

Is this the whole script? Because if it is you’re not connecting the touched event to anything

1 Like

Here’s the whole script:

local Tool = script.Parent
local Remote = Tool:WaitForChild("Remote")
local Handle = Tool:WaitForChild("Handle")

local FriendlyFire = false

local ArmMesh = script.Parent.Handle

local HitAble = false
local HitWindup = 0.15
local HitWindow = 0.75
local HitVictims = {}
local SwingAble = true
local SwingRestTime = 1

function getPlayer()
	local char = Tool.Parent
	return game:GetService("Players"):GetPlayerFromCharacter(char)
end

function contains(t, v)
	for _, val in pairs(t) do
		if val == v then
			return true
		end
	end
	return false
end

function tagHuman(human)
	local tag = Instance.new("ObjectValue")
	tag.Value = getPlayer()
	tag.Name = "creator"
	tag.Parent = human
	game:GetService("Debris"):AddItem(tag)
end

function sameTeam(otherHuman)
	local player = getPlayer()
	local otherPlayer = game:GetService("Players"):GetPlayerFromCharacter(otherHuman.Parent)
	if player and otherPlayer then
		if player == otherPlayer then
			return true
		end
		if otherPlayer.Neutral then
			return false
		end
		return player.TeamColor == otherPlayer.TeamColor
	end
	return false
end

	return not (sameTeam(otherHuman) and not FriendlyFire)
end

function onTouched(part)
	if part:IsDescendantOf(Tool.Parent) then return end
	if not HitAble then return end
	
	if part.Parent and part.Parent:FindFirstChild("Humanoid") then
		local human = part.Parent.Humanoid
		local Player = game.Players:GetPlayerFromCharacter(part.Parent)

		function hitDollars(Player)
			Player.leaderstats.smackDollars.Value = Player.leaderstats.smackDollars.Value + 5
		end
		
		hitDollars(Player)
		
		if contains(HitVictims, human) then return end
		
		local root = part.Parent:FindFirstChild("HumanoidRootPart")
		if root and not root.Anchored then
			local myRoot = Tool.Parent:FindFirstChild("HumanoidRootPart")
			if myRoot and checkTeams(human) then
				local delta = root.Position - myRoot.Position
				
				human.Sit = true
				tagHuman(human)
				table.insert(HitVictims, human)
				
				local bv = Instance.new("BodyVelocity")
				bv.maxForce = Vector3.new(1e9, 1e9, 1e9)
				bv.velocity = delta.unit * 100
				bv.Parent = root
				game:GetService("Debris"):AddItem(bv, 0.05)
				
				Handle.Smack.Pitch = math.random(90, 110)/100
				Handle.Smack.TimePosition = 0.15
				Handle.Smack:Play()
			end
		end
	end
end

function onEquip()
	local char = Tool.Parent
	local arm = Tool.Handle:Clone()
	arm.Parent = char:FindFirstChild("Right Arm")
	ArmMesh = arm
end

function onUnequip()
	if ArmMesh then
		ArmMesh:Destroy()
		ArmMesh = nil
	end
end

function onLeftDown()
	if not SwingAble then return end
	
	SwingAble = false
	delay(SwingRestTime, function()
		SwingAble = true
	end)
	
	delay(HitWindup, function()
		HitAble = true
		delay(HitWindow, function()
			HitAble = false
		end)
	end)
	
	HitVictims = {}
	
	Remote:FireClient(getPlayer(), "PlayAnimation", "Swing")
	
	wait(0.25)
	Handle.Boom:Play()
end

function onRemote(player, func, ...)
	if player ~= getPlayer() then return end
	
	if func == "LeftDown" then
		onLeftDown(...)
	end
end

Tool.Equipped:connect(onEquip)
Tool.Unequipped:connect(onUnequip)
Handle.Touched:connect(onTouched)
Remote.OnServerEvent:connect(onRemote)
1 Like

With the way you have the hitDollars function setup, it’s pretty pointless. You can just remove it, but keep the one line of Player.leaderstats.smackDollars.Value = Player.leaderstats.smackDollars.Value + 5

1 Like

With the new code:

function onTouched(part)
	if part:IsDescendantOf(Tool.Parent) then return end
	if not HitAble then return end
	
	if part.Parent and part.Parent:FindFirstChild("Humanoid") then
		local human = part.Parent.Humanoid
		local Player = game.Players:GetPlayerFromCharacter(part.Parent)
		Player.leaderstats.smackDollars.Value = Player.leaderstats.smackDollars.Value + 5

The output when the tool is used says that I attempted to index nil with ‘leaderstats’ and this happens as well with preexisting folders like Backpack. Am I missing something?

1 Like

Oh okay it just hit me that it’s not the player itself touching the part but something else.

Okay well you already have a function in your script to get the player so you can do this and you can get rid of the line that defines Player

getPlayer().leaderstats.smackDollars.Value = getPlayer().leaderstats.smackDollars.Value + 5
1 Like

Thank you so much for this you’re a really great help.