Hi, I'm making a punch tool but it only works whenever I'm close to an enemy. How can I make it so it works even if I'm not near an enemy? Thanks!

local tool = script.Parent
local debounce = false
local swingsound = tool:WaitForChild("Swing")
local canattack = tool:WaitForChild("CanAttack")
local UIS = game:GetService('UserInputService')
local plr = game.Players.LocalPlayer
repeat wait() until plr.Character ~= nil
local char
local hum
local idle
local root = plr.Character:WaitForChild("HumanoidRootPart")

local debounce = false
local basiccombatdamage = 10

local function damagemarker()
	local bill = Instance.new("BillboardGui")
	bill.Parent = game.Workspace
	bill.Active = false
	bill.Adornee = char.Head
	bill.AlwaysOnTop = false
	bill.Enabled = true
	bill.Size = UDim2.new(1.2, 0, 1.2, 0)
	bill.StudsOffset = Vector3.new(math.random(-3, 3),math.random(-3, 3), math.random(-3, 3))
	local text = Instance.new("TextLabel", bill)
	text.BackgroundTransparency = 1
	text.Name = "TakeDamage"
	text.Size = UDim2.new(1.2, 0, 1.2, 0)
	text.Font = "Fantasy"
	text.FontSize = "Size48"
	text.Text = "10"
	text.TextColor3 = Color3.new(245, 255, 245)
	text.TextScaled = true
	text.TextStrokeColor3 = Color3.new(245, 245, 235)
	text.TextStrokeTransparency = 0
	text.TextWrapped = true
	for i = 1, 25 do
	wait(0.01)
	text.Position = text.Position - UDim2.new(0, 0, 0.04, 0)
	text.TextTransparency = text.TextTransparency + 0.04
	text.TextStrokeTransparency = text.TextStrokeTransparency + 0.04
	end
end

tool.Equipped:connect(function()
	char = char or plr.Character or plr.CharacterAdded:wait()
	hum = hum or char:WaitForChild("Humanoid")
	idle = idle or hum:LoadAnimation(script.Idle)
	idle:Play()
	idle.Looped = true
end)
	
tool.Activated:connect(function()
	if debounce == false then -- check if debounce is false
        local ray = Ray.new(root.CFrame.p, root.CFrame.lookVector * 4)
        local hit, hitposition = workspace:FindPartOnRay(ray, char)
		if hit then
			local humanoid = hit.Parent:FindFirstChild("Humanoid")
			
			if not humanoid then
	           	humanoid = hit.Parent:FindFirstChild("Humanoid")
	        end
		if humanoid ~= nil and (root.Position - hitposition).magnitude <= 50 then
			canattack.Value = true
			local choose = math.random(1,2)
			if choose == 1 then
				debounce = true -- set debounce as true
				local swing1animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch1)
				swingsound:Play()
				swing1animation:Play()
				damagemarker()
				humanoid:TakeDamage(basiccombatdamage)
				wait(.5)
				swingsound:Stop()
				swing1animation:Stop()
				debounce = false -- set debounce as false so the code can run again
			elseif choose == 2 then
				debounce = true -- set debounce as true
				local swing2animation = script.Parent.Parent.Humanoid:LoadAnimation(script.Punch2)
				swingsound:Play()
				swing2animation:Play()
				damagemarker()
				humanoid:TakeDamage(basiccombatdamage)
				wait(.5)
				swingsound:Stop()
				swing2animation:Stop()
				debounce = false -- set debounce as false so the code can run again
			end
		end
		end
	end
end)
	
	tool.Deactivated:connect(function()
		canattack.Value = true
	end)
	
	tool.Unequipped:connect(function()
		idle:Stop()
	end)

2 comments

0

It's only working when close to an enemy because of you're ray and magnitude, the script will only run if the ray finds a humanoid and it's closer than 50 studs. You could add an else statement if they're not close enough, then play the animations.

0

Yep, that's right. I'm only a begginer so I didn't know much about that but thanks, it works.