Any way to make the sword only deal damage when you click?
What do you mean by “sword”? Is it a custom sword you made, or is it something else?
When you make posts like these, it would be preferable if you were able to provide at least a portion of the code. A possible fix is so that whenever the sword is activated, a variable is set for the duration of the attack, before being set once again to false, then when the sword itself is touched, all you need to do is check if the variable is true or not and do the appropriate part of the code. This can also function as an alternative to a debounce.
local isAttacking = false
local attackTime = .5
script.Parent.Activated:connect(function()
if isAttacking then return end isAttacking = not isAttacking
isAttacking = true
-- Play animations and any other code for attack
wait(attackTime) -- assuming you don't already have a wait in the code
isAttacking = false
end)
The above code is an example of what I mean in relation to checking whether the player attacked, for the damage part of the sword’s code, just place a conditional if statement to check whether isAttacking would be false or true.
If you could post your current code that’d be nice.
Otherwise, make a canDamage variable and whenever they click set it to true, when you deal damage make aure its true, if not then do something. Whenever they release click make candamage false.
didn’t really post any code because I’ve haven’t made a sword in a long time and my toolbox inventory is a mess, plus the search bar doesn’t help either. When I find it I’ll give you guys the code
Found the code, it has a script and a local script.
-script-
script.Parent.blade.Touched:connect(function(p)
if script.Parent.CanDamage.Value == true then
script.Parent.CanDamage.Value = false
p.Parent.Humanoid:TakeDamage(20)
end
end)
-local script-
local CanAttack = true
script.Parent.Activated:connect(function()
local attack = script.Parent.Parent.Humanoid:LoadAnimation(script.Attack)
if CanAttack == true then
attack:Play()
CanAttack = false
wait(1)
attack:Stop()
CanAttack = true
script.Parent.CanDamage.Value = true
end
end)
excuse the mess, I’ve been waiting for roblox to invite me for a year or so and I got accepted last 2 weeks so I’m not really familiar with this
Your script doesn’t seem as if it would work? The server-side script is checking if a value is true before setting it to false and then dealing damage, however the value to make it able to deal damage is changed by a local script (client-side), therefore making it so on the server, assuming the value is true when it starts, would be set to false permanently (server-side) after a single touch?
It also seems that your placement of said change to variable is after the attack is over, meaning that it would attempt to make it so damage can be dealt after the attack? Also, this kind of script would be unreliable even if it could work - player’s would change the value of CanDamage to true and put that into a loop so that they can spam damage without having to click.
I should probably just make a new one from scratch.
Gonna spend all summer vacations learning lua and hopefully be able to script something decent.
Alright - make sure to never trust anything sent from the client and make all the necessary checks server-side to confirm things, especially when using RemoteEvents and RemoteFunctions, which are used to communicate between the client and server.