Gun can't register damage to player

local Tool = script.Parent
local Fire = Tool:WaitForChild("Fire")

-- Анимации
local idleAnimId = "rbxassetid://113335815798188"   -- idle anim
local attackAnimId = "rbxassetid://112107120372085" -- attack anim

local idleAnim = Instance.new("Animation")
idleAnim.AnimationId = idleAnimId

local attackAnim = Instance.new("Animation")
attackAnim.AnimationId = attackAnimId

local currentIdleTrack
local currentAttackTrack

local fireSound = Tool.FireSound

local canFire = true
local cooldownTime = 0.5 -- cooldown erm

Tool.Equipped:Connect(function()
	local char = Tool.Parent
	local humanoid = char:FindFirstChildOfClass("Humanoid")
	if humanoid then
		currentIdleTrack = humanoid:LoadAnimation(idleAnim)
		currentIdleTrack.Looped = true
		currentIdleTrack:Play()
	end
end)


Tool.Unequipped:Connect(function()
	if currentIdleTrack then
		currentIdleTrack:Stop()
		currentIdleTrack:Destroy()
		currentIdleTrack = nil
	end
	if currentAttackTrack then
		currentAttackTrack:Stop()
		currentAttackTrack:Destroy()
		currentAttackTrack = nil
	end
end)

Fire.OnServerEvent:Connect(function(player, target)
	if not canFire then return end
	canFire = false

	local char = Tool.Parent
	local humanoid = char:FindFirstChildOfClass("Humanoid")
	if not humanoid then 
		canFire = true
		return 
	end


	if target and target:IsA("BasePart") then
		local model = target:FindFirstAncestorOfClass("Model")
		if model and model:FindFirstChildOfClass("Humanoid") then
			target = model
		else
			target = nil
		end
	end
	
	if target and target:FindFirstChild("Humanoid") then
		target.Humanoid:TakeDamage(15)
	end

	if fireSound then
		fireSound:Play()
	end

	if currentIdleTrack then
		currentIdleTrack:Stop()
		currentIdleTrack:Destroy()
		currentIdleTrack = nil
	end

	currentAttackTrack = humanoid:LoadAnimation(attackAnim)
	currentAttackTrack.Looped = false
	currentAttackTrack:Play()

	currentAttackTrack.Stopped:Connect(function()
		if Tool.Parent == char and humanoid.Health > 0 then
			if not currentIdleTrack then
				currentIdleTrack = humanoid:LoadAnimation(idleAnim)
				currentIdleTrack.Looped = true
				currentIdleTrack:Play()
			end
		end
	end)

	task.delay(cooldownTime, function()
		canFire = true
	end)
end)

You can insert print() in each section to detect where in your script the problem is.

1 Like

Thanks i fixed bug, solution is humanoidrootpart

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