Part Damaging more than Once

When the part is touched it is supposed to damage the player once but I can’t figure out how to make the part only damage the player once. Here is the server script.

local fireballRM = game.ReplicatedStorage.Fireball
local tool = script.Parent
local addCoins = require(game.ReplicatedStorage.CoinFunctions)
local canUseAbility = true

local playerService = game:GetService("Players")

fireballRM.OnServerEvent:Connect(function(plr)
	if tool.Parent == plr.Character and canUseAbility then

		canUseAbility = false
		local Sound = Instance.new("Sound", game.Workspace)
		Sound.Volume = 0.5
		Sound.Looped = false
		Sound.SoundId = "rbxassetid://5312934759"
		Sound.Volume = 0.5
		Sound:Play()
		local HRP = plr.Character.HumanoidRootPart

		local Part = Instance.new("Part" ,workspace) -- the part to move
		local flame = Instance.new("Fire", Part)
		Part.CanCollide = false
		Part.Shape = Enum.PartType.Ball
		Part.CFrame = HRP.CFrame -- Putting the part infront of the player
		Part.Color = Color3.new(1, 0, 0)
		Part.Material = Enum.Material.Neon


		local Attachment = Instance.new("Attachment", Part) -- the attachment used in Linearvelocity

		local LV = Instance.new("LinearVelocity", Attachment) -- creating the linear velocity
		LV.MaxForce = math.huge -- no need to worry about this
		LV.VectorVelocity = HRP.CFrame.lookVector * 100 -- change 100 with how fast you want the projectile to go
		LV.Attachment0 = Attachment -- setting the attachment

		Part.Touched:Connect(function(hit)
			local canDmg = true
			if hit.Parent.Humanoid ~= plr.Character.Humanoid and canDmg then 

				
				addCoins.DamageReward(5, 1, playerService:GetPlayerFromCharacter(tool.Parent))
				if canDmg then
					hit.Parent.Humanoid:TakeDamage()
				end
				
				Part:Destroy()
			end


		end)


		game.Debris:AddItem(Part, 3) -- deletes the moving part after 2 second

		task.wait(15)
		canUseAbility = true


	end
end)

Thanks for any help.

1 Like

Set canDmg to false inside of the Touched connected function. Also, you can remove the second if canDmg because you already check for it in the outer if.

...
local canDmg = true
Part.Touched:Connect(function(hit)
	if hit.Parent.Humanoid ~= plr.Character.Humanoid and canDmg then
		canDmg = false
		addCoins.DamageReward(5, 1, playerService:GetPlayerFromCharacter(tool.Parent))
		hit.Parent.Humanoid:TakeDamage()
		Part:Destroy()
	end
end)
...

edit:
Move canDmg outside of the function so it isn’t reinitialized to true.

2 Likes

I get an error saying “Argument 1 is missing or nil”?

1 Like

On which line? Please paste the code as well.

1 Like
Part.Touched:Connect(function(hit)
			if hit.Parent.Humanoid ~= plr.Character.Humanoid and canDmg then
				canDmg = false
				addCoins.DamageReward(5, 1, playerService:GetPlayerFromCharacter(tool.Parent))
				hit.Parent.Humanoid:TakeDamage() -- THIS LINE HAS ERROR
				Part:Destroy()
			end
		end)

Perhaps you should provide a damage amount as input to TakeDamage().

Ive tried using hit.Parent.Humanoid.Health -= 10 and that works but its an error with :TakeDamage()

ohhhh that was a dumb mistake lol thanks

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.