Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to fix my Sword script so it doesn't attack when I'm not clicking?

Before the first click, when I touch the monster without clicking, the monster's hp doesn't go down. But, after the first click, whenever I touch the monster without clicking, the monster's hp will decrease. How to fix it?

PlayerName = script.Parent.Parent.Parent.Name
Tool = script.Parent
Damage = script.Parent.Settings.Damage
local ButtonPressed = false

Tool.Equipped:connect(function(Mouse)
	Mouse.Button1Down:connect(function()
----------------------------------------------------------------------	
		script.Parent.DamagePart.Touched:connect(function(hit)
			if not ButtonPressed then
				--is it not pressed?
				ButtonPressed = true
				-----------------------
				if hit.Parent then
				local human = hit.Parent
				hum = human:FindFirstChild("Settings")
				if hum then
					hum.HP.Value = hum.HP.Value - script.Parent.Settings.Damage.Value
					
					x = hit.Parent:FindFirstChild("Fighters")
					y = x:FindFirstChild(PlayerName)
					if not y then
						z = Instance.new("IntValue")
						z.Parent = x
						z.Name = PlayerName
					end
					
				end
				
				wait(1)
				end
				------------------------
				wait()
				ButtonPressed = false
			end
		end)
---------------------------------------------------------------------
	end)
end)

1 answer

PlayerName = script.Parent.Parent.Parent.Name
Tool = script.Parent
Damage = script.Parent.Settings.Damage
local ButtonPressed = true  

Tool.Equipped:connect(function(Mouse)
    Mouse.Button1Down:connect(function()
----------------------------------------------------------------------  
        script.Parent.DamagePart.Touched:connect(function(hit)
           if not debounce then
       		return
    		end
		ButtonPressed = false
                -----------------------
                if hit.Parent then
                local human = hit.Parent
                hum = human:FindFirstChild("Settings")
                if hum then
                    hum.HP.Value = hum.HP.Value - script.Parent.Settings.Damage.Value

                    x = hit.Parent:FindFirstChild("Fighters")
                    y = x:FindFirstChild(PlayerName)
                    if not y then
                        z = Instance.new("IntValue")
                        z.Parent = x
                        z.Name = PlayerName
                    end

                wait(1)
                end
                ------------------------
                wait()
                ButtonPressed = false
            end
        end)
---------------------------------------------------------------------
    end)
end)

2 comments

0

Let me know if this works. I am currently unable to test my code so there might be a small error.

0

You mean ButtonPressed = debounce? and turn the last ButtonPressed into true? Still not working, after the first click, whenever the sword touchs the monster even when I don't click the button1, It attacks..

This discussion is preserved as it was. New answers, comments, and votes are closed.