How do you give a specified player cash through a script?

Hi, so in my towers they have a script when they’re placed they have a creator value being valued to the players name, and i made so that it gives cash everytime each shot they do, but somehow thats not always the case, somehow it makes error like ‘‘nil with leaderstats’’, so what is the exactly accurate way to fix this?

if target and player ~=	nil and script.Parent.Placed.Value == true then
				
				local playervalues = game.Players:FindFirstChild(player)

				script.Parent.Torso.CFrame = CFrame.new(script.Parent.Torso.Position,target.Torso.Position)
				target.Zombie:TakeDamage(script.Parent.CurrentDamage.Value/target.Defense.Value)
			--	towertorso.Parent:SetPrimaryPartCFrame(CFrame.new(script.Parent.PrimaryPart.Position,target.Torso.Position))
		    	playervalues.leaderstats.Coins.Value = playervalues.leaderstats.Coins.Value + script.Parent.CurrentDamage.Value
				FireSound.PlaybackSpeed = 1
				FireSound.SoundId = "rbxassetid://"..script.Parent.GunSound_ID.Value
				FireSound.Parent = script.Parent.Torso
				FireSound.Volume = 2
				FireSound.TimePosition = 0
				FireSound:Play()
				humanim:Play()
				print(damagenumber,'DMG to', target.Name)
			
			end

it does give money though, but the problem is i need to solve it universally so it doesn’t make errors for other situation such as when they are stunned or deploy units near them. the error comes the whole time.

Add a function that triggers everytime you place the object

:yourfunction(plr) or :yourfunction(player) you can get the LocalPlayer

task.wait(0.25)
if target and player ~=	nil and script.Parent.Placed.Value == true then
				
				local playervalues = game.Players:FindFirstChild(player)

				script.Parent.Torso.CFrame = CFrame.new(script.Parent.Torso.Position,target.Torso.Position)
				target.Zombie:TakeDamage(script.Parent.CurrentDamage.Value/target.Defense.Value)
			--	towertorso.Parent:SetPrimaryPartCFrame(CFrame.new(script.Parent.PrimaryPart.Position,target.Torso.Position))
		    	playervalues:WaitForChild(“leaderstats”):WaitForChild(“Coins”).Value 
 += script.Parent:WaitForChild(“CurrentDamage”).Value
				FireSound.PlaybackSpeed = 1
				FireSound.SoundId = "rbxassetid://"..script.Parent.GunSound_ID.Value
				FireSound.Parent = script.Parent.Torso
				FireSound.Volume = 2
				FireSound.TimePosition = 0
				FireSound:Play()
				humanim:Play()
				print(damagenumber,'DMG to', target.Name)
			
			end

Add some WaitForChilds because values can be tricky.

now its attempting to index nil with WaitForChild(), but it still shoots and gives money, but it looks weird cause it doesn’t have animation

Try surrounding @Redluo’s code with a pcall function like this :point_down::point_down::point_down:

task.wait(0.25)

local success, errormessage = pcall(function()
if target and player ~=	nil and script.Parent.Placed.Value == true then
				
				local playervalues = game.Players:FindFirstChild(player)

				script.Parent.Torso.CFrame = CFrame.new(script.Parent.Torso.Position,target.Torso.Position)
				target.Zombie:TakeDamage(script.Parent.CurrentDamage.Value/target.Defense.Value)
			--	towertorso.Parent:SetPrimaryPartCFrame(CFrame.new(script.Parent.PrimaryPart.Position,target.Torso.Position))
		    	playervalues:WaitForChild(“leaderstats”):WaitForChild(“Coins”).Value 
 += script.Parent:WaitForChild(“CurrentDamage”).Value
				FireSound.PlaybackSpeed = 1
				FireSound.SoundId = "rbxassetid://"..script.Parent.GunSound_ID.Value
				FireSound.Parent = script.Parent.Torso
				FireSound.Volume = 2
				FireSound.TimePosition = 0
				FireSound:Play()
				humanim:Play()
				print(damagenumber,'DMG to', target.Name)
			
			end
end)

now its not even shooting, imma try some slander around the code.

Maybe try adding the pcall function where the error occurs. Like this :point_down::point_down::point_down:

task.wait(0.25)
if target and player ~=	nil and script.Parent.Placed.Value == true then
				
				local playervalues = game.Players:FindFirstChild(player)

				script.Parent.Torso.CFrame = CFrame.new(script.Parent.Torso.Position,target.Torso.Position)
				target.Zombie:TakeDamage(script.Parent.CurrentDamage.Value/target.Defense.Value)
			--	towertorso.Parent:SetPrimaryPartCFrame(CFrame.new(script.Parent.PrimaryPart.Position,target.Torso.Position))
local success, errormessage = pcall(function()  	
playervalues:WaitForChild(“leaderstats”):WaitForChild(“Coins”).Value 
 += script.Parent:WaitForChild(“CurrentDamage”).Value
end)
				FireSound.PlaybackSpeed = 1
				FireSound.SoundId = "rbxassetid://"..script.Parent.GunSound_ID.Value
				FireSound.Parent = script.Parent.Torso
				FireSound.Volume = 2
				FireSound.TimePosition = 0
				FireSound:Play()
				humanim:Play()
				print(damagenumber,'DMG to', target.Name)
			
			end

When I put this in the editor to debug it, many things were not definded. This isn’t a full script. You’ll have to define things to get it to work or have anyone help debug it other than just guessing at the problem.

It’s only a chunk of the script.
The part that is broken.

maybe … as a test. Could be sending hits from none players.

  If game.Players:FindFirstChild(player) ~= nil then
      local playervalues = game.Players:FindFirstChild(player)
      ...
      ...
  end