How do i stop a player from getting infinite cash

Hello so basically i’ve got this script here:

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = ReplicatedStorage:WaitForChild('ShotEvent')
local Particle = game.ReplicatedStorage.ParticleEmitter
local hit = ReplicatedStorage.Hit
local damage_amount = 14


remoteEvent.OnServerEvent:Connect(function(player, gunPos, gunOr, mosPos)
	local leadestats = player.leaderstats
	local goldStats = leadestats and leadestats:FindFirstChild("Gold")


	local bullet = Instance.new("Part")

	bullet.Name = 'Bullet'
	bullet.Parent = game.Workspace.Bullets
	bullet.Size = Vector3.new(0.25, 0.25, 0.25)
	bullet.BrickColor = BrickColor.new('White')
	bullet.Shape = Enum.PartType.Ball
	
	local speed = 250
	bullet.CFrame = CFrame.new(gunPos, mosPos)
	bullet.Velocity = bullet.CFrame.lookVector * speed


	local particle = Particle:Clone()

	bullet.Touched:Connect(function(otherPart)
		local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
		if humanoid and humanoid.Parent.Name ~= player.Name then
			if humanoid.Health > 0 and humanoid.Health - damage_amount <= 0 then
			end
			humanoid:TakeDamage(damage_amount)
			if goldStats then
				goldStats.Value += 20
			hit:Play()
			if humanoid.Health <=0 then
				player.PlayerGui.KillGui.Enabled = true
				player.PlayerGui.KillGui.TextLabel.Text = "Killed: " .. otherPart.Parent.Name
				wait(3)
				player.PlayerGui.KillGui.Enabled = false
				Particle.Parent = otherPart:FindFirstChild("Head")
				Particle.Enabled = true
				game:GetService('Debris'):AddItem(Particle, 1) --adjust time before particle gets destroyed
			end
			bullet:Destroy()
		end
	end



		game:GetService('Debris'):AddItem(bullet, 1)
	end)
end)

and theres a line

humanoid:TakeDamage(damage_amount)
			if goldStats then
				goldStats.Value += 20

but what i’ve noticed is that after the player’s dead you can still shoot them and have even more money. How would i fix that?

just check if the humanoid has 0 health with an if statement or check if the humanoid is dead with humanoid.Died function

theres already a line that checks if the humanoid has 0 health, but i just don’t know how to stop the script from giving player more cash.

You’re going to want to check if the Humanoid is alive before you give the gold.

if humanoid.Health > 0 and goldStats then

Also, if you’re doing it this way, make sure to move the :TakeDamage() after you give the gold, or you won’t get gold for the fatal blow.

So should it look something like this?

			if humanoid.Health > 0 and humanoid.Health - damage_amount <= 0 and goldStats then
			end
			if goldStats then
				goldStats.Value += 20
				humanoid:TakeDamage(damage_amount)

Not sure why you added an “end” immediatly after the condition. It’s as if the condition wasn’t there.

No. The placement end statement you have there creates an empty if statement, meaning nothing changes regardless of whether the condition is met.
Something like this would be best.

if humanoid.Health > 0 then
        if humanoid.Health - damage_amount <= 0 and goldStats  then
        	goldStats.Value += 20
        end
	humanoid:TakeDamage(damage_amount)
end

i tried that and now the script doesn’t even deal damage lol.

Whoops, misunderstood how your scirpt worked. Try the edit.

i don’t understand why but the “game” keeps getting a red line under it

image

There’s probably a syntax error before that line

1 Like

Maybe something like this.

A few changes
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = ReplicatedStorage:WaitForChild('ShotEvent')
local Particle = game.ReplicatedStorage.ParticleEmitter
local hit = ReplicatedStorage.Hit
local damage_amount = 14

remoteEvent.OnServerEvent:Connect(function(player, gunPos, gunOr, mosPos)
    local leadestats = player:FindFirstChild("leaderstats")
    local goldStats = leadestats and leadestats:FindFirstChild("Gold")

    local bullet = Instance.new("Part")
    bullet.Name = 'Bullet'
    bullet.Parent = game.Workspace.Bullets
    bullet.Size = Vector3.new(0.25, 0.25, 0.25)
    bullet.BrickColor = BrickColor.new('White')
    bullet.Shape = Enum.PartType.Ball
    
    local speed = 250
    bullet.CFrame = CFrame.new(gunPos, mosPos)
    bullet.Velocity = bullet.CFrame.lookVector * speed

    local particle = Particle:Clone()

    bullet.Touched:Connect(function(otherPart)
        local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
        if humanoid and humanoid.Parent.Name ~= player.Name then
            if humanoid.Health > 0 and humanoid.Health - damage_amount <= 0 then
                humanoid:TakeDamage(damage_amount)
                if goldStats then
                    goldStats.Value = goldStats.Value + 20
                end
                hit:Play()
                if humanoid.Health <= 0 then
                    player.PlayerGui.KillGui.Enabled = true
                    player.PlayerGui.KillGui.TextLabel.Text = "Killed: " .. otherPart.Parent.Name
                    wait(3)
                    player.PlayerGui.KillGui.Enabled = false
                    Particle.Parent = otherPart:FindFirstChild("Head")
                    Particle.Enabled = true
                    game:GetService('Debris'):AddItem(Particle, 1)
                end
                bullet:Destroy()
            end
        end
    end)

    game:GetService('Debris'):AddItem(bullet, 1)
end)

tried that and the gun doesn’t even deal damage.

I just posted that. Are you sure you tried that script.

uhhhhhh yeah? i just copied and pasted that script.

ok ok lol
I’ll give it one more shot …

A few modifications
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = ReplicatedStorage:WaitForChild('ShotEvent')
local Particle = game.ReplicatedStorage.ParticleEmitter
local hit = ReplicatedStorage.Hit
local damage_amount = 14

remoteEvent.OnServerEvent:Connect(function(player, gunPos, gunOr, mosPos)
    local leadestats = player:FindFirstChild("leaderstats")
    local goldStats = leadestats and leadestats:FindFirstChild("Gold")

    local bullet = Instance.new("Part")
    bullet.Name = 'Bullet'
    bullet.Parent = game.Workspace.Bullets
    bullet.Size = Vector3.new(0.25, 0.25, 0.25)
    bullet.BrickColor = BrickColor.new('White')
    bullet.Shape = Enum.PartType.Ball
    
    local speed = 250
    bullet.CFrame = CFrame.new(gunPos, mosPos)
    bullet.Velocity = bullet.CFrame.lookVector * speed

    local particle = Particle:Clone()

    bullet.Touched:Connect(function(otherPart)
        local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
        if humanoid and humanoid.Parent.Name ~= player.Name then
            if humanoid.Health > 0 then
                humanoid:TakeDamage(damage_amount)
                if goldStats then
                    goldStats.Value = goldStats.Value + 20
                end
                hit:Play()
                if humanoid.Health <= 0 then
                    player.PlayerGui.KillGui.Enabled = true
                    player.PlayerGui.KillGui.TextLabel.Text = "Killed: " .. otherPart.Parent.Name
                    wait(3)
                    player.PlayerGui.KillGui.Enabled = false
                    Particle.Parent = otherPart:FindFirstChild("Head")
                    Particle.Enabled = true
                    game:GetService('Debris'):AddItem(Particle, 1)
                end
                bullet:Destroy()
            end
        end
    end)

    game:GetService('Debris'):AddItem(bullet, 1)
end)

Still may be off a bit I have no way to test this. More looking for a different way to figure it.

i got a funny fact for you, wanna know it?

Yes I know it’s a bit sloppy. You’ll have to fine tune to your liking.

the script actually worked with no errors lol :+1: (so yeah thank you all for helping)

Sorry, I just woke up that wasn’t the best logic the 1st time … (my bad)