How to give all players cash?

Hello! I am trying to give all players cash when a mob is killed and dont know how!
Heres the code:

function tower.Attack(newTower, player)
	local config = newTower.Config
	local target = tower.FindTarget(newTower, config.Range.Value, config.TargetMode.Value)
	if target and target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 then
		local targetCFrame = CFrame.lookAt(newTower.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
		newTower.HumanoidRootPart.BodyGyro.CFrame = targetCFrame

		animateTowerEvent:FireAllClients(newTower, "Attack", target)

		newTower.FireSound:Play()

		target.Humanoid:TakeDamage(config.Damage.Value)

		if target.Humanoid.Died then
			player.Money.Value += target.Humanoid.MaxHealth
			
		end

		task.wait(config.Cooldown.Value)
	end

	task.wait(0.1)

	if newTower and newTower.Parent then
		tower.Attack(newTower, player)
	end
end

You can loop through every player, and add to their cash value. It would look something like this:

local amount = 50

for _, plr in pairs(game.Players:GetPlayers()) do
    plr.leaderstats.Cash.Value += amount
end

I get this error

Heres how I implemented the code:

function tower.Attack(newTower, player)
	local amount = 50
	local config = newTower.Config
	local target = tower.FindTarget(newTower, config.Range.Value, config.TargetMode.Value)
	if target and target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 then
		local targetCFrame = CFrame.lookAt(newTower.HumanoidRootPart.Position, target.HumanoidRootPart.Position)
		newTower.HumanoidRootPart.BodyGyro.CFrame = targetCFrame

		animateTowerEvent:FireAllClients(newTower, "Attack", target)

		newTower.FireSound:Play()

		target.Humanoid:TakeDamage(config.Damage.Value)

		if target.Humanoid.Died then
			for _, plr in pairs(game.Players:GetPlayers()) do
				plr.leaderstats.Cash.Value += amount
		end
	end
		task.wait(config.Cooldown.Value)
	end

	task.wait(0.1)

	if newTower and newTower.Parent then
		tower.Attack(newTower, player)
	end
end

Well how are you storing your cash, because it’s clearly not leaderstats. Maybe try:

local amount = 50

for _, plr in pairs(game.Players:GetPlayers()) do
    plr.Cash.Value += amount
end
1 Like

Make sure to check if plr.Cash is even a thing, otherwise it will error if Cash was not found. :slight_smile: