Can anyone explain to me why this damage script wont work? It works the first time but when I activate a different tool and use this tool again, the damage doesnt work.
r = game:service("RunService")
local damage = 0
sword = script.Parent.Handle
Tool = script.Parent
local damages,values,sounds = {15,20,25},{Tool.PlaySlash,Tool.PlayThrust,Tool.PlayOverhead},{Tool.Handle.SlashSound,Tool.Handle.OverheadSound,Tool.Handle.LungeSound}
local enabledToDamage = true
function blow(hit)
if enabledToDamage == false then return end
enabledToDamage = false
if (hit.Parent == nil) then enabledToDamage = true return end -- happens when bullet hits sword
local humanoid = hit.Parent:findFirstChild("Humanoid")
local vCharacter = Tool.Parent
local vPlayer = game.Players:playerFromCharacter(vCharacter)
local hum = vCharacter:findFirstChild("Humanoid") -- non-nil if tool held by a character
if humanoid~=nil and humanoid ~= hum and hum ~= nil then
-- final check, make sure sword is in-hand
local right_arm = vCharacter:FindFirstChild("Right Arm")
if (right_arm ~= nil) then
local joint = right_arm:FindFirstChild("RightGrip")
if (joint ~= nil and (joint.Part0 == sword or joint.Part1 == sword)) then
tagHumanoid(humanoid, vPlayer)
humanoid:TakeDamage(damage)
wait(1)
untagHumanoid(humanoid)
else
enabledToDamage = true
end
else
enabledToDamage = true
end
else
enabledToDamage = true
end
end
function tagHumanoid(humanoid, player)
local creator_tag = Instance.new("ObjectValue")
creator_tag.Value = player
creator_tag.Name = "creator"
creator_tag.Parent = humanoid
end
function untagHumanoid(humanoid)
if humanoid ~= nil then
local tag = humanoid:findFirstChild("creator")
if tag ~= nil then
tag.Parent = nil
end
end
end
function attack()
damage = slash_damage
script.Parent.Handle.SlashSound:Play()
script.Parent.PlaySlash.Value = not script.Parent.PlaySlash.Value
end
function lunge()
damage = lunge_damage
script.Parent.Handle.LungeSound:Play()
script.Parent.PlayOverhead.Value = not script.Parent.PlayOverhead.Value
force = Instance.new("BodyVelocity")
force.velocity = Vector3.new(0,10,0) --Tool.Parent.Torso.CFrame.lookVector * 80
force.Parent = Tool.Parent.Torso
wait(.5)
force.Parent = nil
wait(.5)
damage = slash_damage
end
Tool.Enabled = true
local last_attack = 0
local status = 0
function onActivated()
if not Tool.Enabled then
return
end
Tool.Enabled = false
local character = Tool.Parent;
local humanoid = character.Humanoid
if humanoid == nil then
print("Humanoid not found")
return
end
t = r.Stepped:wait()
if (t - last_attack < 1.5) then
if status == 3 then
status = 0
damage = 0
else
status = status + 1
values[status].Value = not values[status].Value
damage = damages[status]
sounds[status]:Play()
enabledToDamage = true
wait(0.5)
enabledToDamage = false
end
else
status = 0
damage = 0
end
last_attack = t
Tool.Enabled = true
end
function onEquipped()
wait(1/3)
Tool.Handle.UnsheathSound:Play()
end
Tool.Equipped:Connect(function()
onEquipped()
end)
Tool.Activated:connect(onActivated)
connection = sword.Touched:connect(blow)
local r = game:GetService("RunService")
local damage = 0
local sword = script.Parent.Handle
local Tool = script.Parent
local damages = {15, 20, 25}
local values = {Tool.PlaySlash, Tool.PlayThrust, Tool.PlayOverhead}
local sounds = {Tool.Handle.SlashSound, Tool.Handle.OverheadSound, Tool.Handle.LungeSound}
local enabledToDamage = true
function blow(hit)
if not enabledToDamage then return end
enabledToDamage = false
if hit.Parent == nil then
enabledToDamage = true
return
end
local humanoid = hit.Parent:FindFirstChild("Humanoid")
local vCharacter = Tool.Parent
local vPlayer = game.Players:GetPlayerFromCharacter(vCharacter)
local hum = vCharacter:FindFirstChild("Humanoid")
if humanoid and humanoid ~= hum and hum then
local right_arm = vCharacter:FindFirstChild("Right Arm")
if right_arm then
local joint = right_arm:FindFirstChild("RightGrip")
if joint and (joint.Part0 == sword or joint.Part1 == sword) then
tagHumanoid(humanoid, vPlayer)
humanoid:TakeDamage(damage)
wait(1)
untagHumanoid(humanoid)
end
end
end
enabledToDamage = true
end
function tagHumanoid(humanoid, player)
local creator_tag = Instance.new("ObjectValue")
creator_tag.Value = player
creator_tag.Name = "creator"
creator_tag.Parent = humanoid
end
function untagHumanoid(humanoid)
if humanoid then
local tag = humanoid:FindFirstChild("creator")
if tag then
tag:Destroy()
end
end
end
function attack()
damage = damages[1]
script.Parent.Handle.SlashSound:Play()
script.Parent.PlaySlash.Value = not script.Parent.PlaySlash.Value
end
function lunge()
damage = damages[3]
script.Parent.Handle.LungeSound:Play()
script.Parent.PlayOverhead.Value = not script.Parent.PlayOverhead.Value
local force = Instance.new("BodyVelocity")
force.Velocity = Vector3.new(0, 10, 0)
force.Parent = Tool.Parent.Torso
wait(0.5)
force:Destroy()
wait(0.5)
damage = damages[1]
end
Tool.Enabled = true
local last_attack = 0
local status = 0
function onActivated()
if not Tool.Enabled then return end
Tool.Enabled = false
local character = Tool.Parent
local humanoid = character:FindFirstChild("Humanoid")
if not humanoid then
print("Humanoid not found")
return
end
local t = r.Stepped:Wait()
if t - last_attack < 1.5 then
if status == 3 then
status = 0
damage = 0
else
status = status + 1
values[status].Value = not values[status].Value
damage = damages[status]
sounds[status]:Play()
end
else
status = 0
damage = 0
end
last_attack = t
Tool.Enabled = true
end
function onEquipped()
wait(1/3)
Tool.Handle.UnsheathSound:Play()
end
Tool.Equipped:Connect(onEquipped)
Tool.Activated:Connect(onActivated)
sword.Touched:Connect(blow)
Tool.Unequipped:Connect(function()
enabledToDamage = true
damage = 0
status = 0
end)
try this. Theres quite a few mistakes in the script which were likely causing the issue
local r = game:GetService("RunService")
local damage = 0
local sword = script.Parent.Handle
local Tool = script.Parent
local damages = {15, 20, 25}
local values = {Tool.PlaySlash, Tool.PlayThrust, Tool.PlayOverhead}
local sounds = {Tool.Handle.SlashSound, Tool.Handle.OverheadSound, Tool.Handle.LungeSound}
local enabledToDamage = true
local debounce = false
function blow(hit)
if not enabledToDamage or debounce then return end
enabledToDamage = false
if hit.Parent == nil then
enabledToDamage = true
return
end
local humanoid = hit.Parent:FindFirstChild("Humanoid")
local vCharacter = Tool.Parent
local vPlayer = game.Players:GetPlayerFromCharacter(vCharacter)
local hum = vCharacter:FindFirstChild("Humanoid")
if humanoid and humanoid ~= hum and hum then
local right_arm = vCharacter:FindFirstChild("Right Arm")
if right_arm then
local joint = right_arm:FindFirstChild("RightGrip")
if joint and (joint.Part0 == sword or joint.Part1 == sword) then
tagHumanoid(humanoid, vPlayer)
humanoid:TakeDamage(damage)
wait(1)
untagHumanoid(humanoid)
end
end
end
enabledToDamage = true
end
function tagHumanoid(humanoid, player)
local creator_tag = Instance.new("ObjectValue")
creator_tag.Value = player
creator_tag.Name = "creator"
creator_tag.Parent = humanoid
end
function untagHumanoid(humanoid)
if humanoid then
local tag = humanoid:FindFirstChild("creator")
if tag then
tag:Destroy()
end
end
end
function attack()
if debounce then return end
debounce = true
damage = damages[1]
script.Parent.Handle.SlashSound:Play()
script.Parent.PlaySlash.Value = not script.Parent.PlaySlash.Value
wait(0.5)
debounce = false
end
function lunge()
if debounce then return end
debounce = true
damage = damages[3]
script.Parent.Handle.LungeSound:Play()
script.Parent.PlayOverhead.Value = not script.Parent.PlayOverhead.Value
local force = Instance.new("BodyVelocity")
force.Velocity = Vector3.new(0, 10, 0)
force.Parent = Tool.Parent.Torso
wait(0.5)
force:Destroy()
wait(0.5)
damage = damages[1]
debounce = false
end
Tool.Enabled = true
local last_attack = 0
local status = 0
function onActivated()
if not Tool.Enabled or debounce then return end
debounce = true
Tool.Enabled = false
local character = Tool.Parent
local humanoid = character:FindFirstChild("Humanoid")
if not humanoid then
print("Humanoid not found")
return
end
local t = r.Stepped:Wait()
if t - last_attack < 1.5 then
if status == 3 then
status = 0
damage = 0
else
status = status + 1
values[status].Value = not values[status].Value
damage = damages[status]
sounds[status]:Play()
end
else
status = 0
damage = 0
end
last_attack = t
Tool.Enabled = true
debounce = false
end
function onEquipped()
wait(1/3)
Tool.Handle.UnsheathSound:Play()
end
Tool.Equipped:Connect(onEquipped)
Tool.Activated:Connect(onActivated)
sword.Touched:Connect(blow)
Tool.Unequipped:Connect(function()
enabledToDamage = true
damage = 0
status = 0
debounce = false
end)
its hard to fully understand without actually seeing the issue but you can try this. if it doesnt work then its out of my knowledge
I’m not sure why it’s not working from looking at the code. I would try reseting the state variables on equip.
If that doesn’t work, it probably would narrow down the problem to add print statements to see where the code stops before dealing the damage (I would guess the state variables are getting muddled somewhere when it’s unequipped and permanently triggering one of your debounces).
Seems like code from an old roblox gear. I’d grab a gear similar to what you are using, and go from there if you want to retain similarity as there are many gears similar to what is shown, even down to the animations.
Although - the code shown is heavily dated, having pointless brackets, using wait (instead of task.wait()), using false over nil (save a little memory), global variables being used instead of local variables, functions being used instead of local functions and outdated ways of calling various RBXScriptSignals. amongst others.
Used the script you provided and altered it, grabbed another tool from the toolbox and used it, sword seems to work, is this the intended behaviour you desire?
Update: I tested it again in a different world, turns out that the script I was using works HOWEVER, it only doesn’t work when I use the tools that I made. Turns out it doesn’t matter if the other tool is activated.
These are the codes that stopped the weapon from doing damage after use:
Coin script:
local player = game.Players.LocalPlayer
local tool = script.Parent
local ui = tool:WaitForChild("CoinGui")
tool.Equipped:Connect(function()
ui.Parent = player
ui.Enabled = true
end)
tool.Unequipped:Connect(function()
ui.Parent = tool
ui.Enabled = false
end)
tool.Activated:Connect(function()
local stat = player:WaitForChild("ahfpoaewhr")
stat.Value += 1
tool.Parent = workspace
tool:Destroy()
end)
Food script:
local Eaten = false
script.Parent.Activated:Connect(function()
if game.Players.LocalPlayer:WaitForChild("Hunger").Value < 200 then
if Eaten then
return
end
Eaten = true
wait(1)
game.ReplicatedStorage.Eat:FireServer()
script.Parent:Destroy()
end
end)
This was the case if you had volume up hearing the audio queue, there was just no animations. As for the current problem, not so sure, I recommend using the dev console or providing the rbxm files so it makes identifying the problems easier.
My hunch is the Eat:FireServer() or destroying the tools while still having them equipped, not sure if the destroy calls unequipped, but if not, the ui is still remaining parented to the player when it is meant to be reparented back using the unequipped connection. So in lots of cases, games use Humanoid:UnequipTools() right before they destroy the tool to stop say, animations from persisting or stay stuck playing at a certain part.
I tested it out and made the tool’s parent to workspace after clicking and then destroying, and the tool works fine now. Thank you so much for suggesting this idea I wouldn’t have ever thought of this