Tool not works when activated

You can write your topic however you want, but you need to answer these questions:

  1. when i active tool it not starts anim not play and hitbox not actives

heres the script

local rs = game:GetService("ReplicatedStorage")
local rc = require(rs.Modules.RaycastHitboxV4)
repeat wait() until script.Parent.Parent:WaitForChild("Humanoid")
local hum = script.Parent.Parent:WaitForChild("Humanoid")
local AnimFolder = script.Parent.Anims
local slash = hum:LoadAnimation(AnimFolder.Slash)
local slash2 = hum:LoadAnimation(AnimFolder.Slash2)
local slash3 = hum:LoadAnimation(AnimFolder.Slash3)
local randomAnim = {slash, slash2, slash3}
local anim = randomAnim[math.random(1, #randomAnim)]
local db = false
local new = rc.new(script.Parent.Handle)

local Params = RaycastParams.new()
Params.FilterDescendantsInstances = {hum.Parent} --- remember to define our character!
Params.FilterType = Enum.RaycastFilterType.Blacklist

new.RaycastParams = Params

new.OnHit:Connect(function(hit)
	local hum = hit.Parent:WaitForChild("Humanoid")
	hum:TakeDamage(10)
	hum.Died:Connect(function()
		rs:WaitForChild("Events").BindableEvents.KillEffect:Fire(hit)
	end)
end)


script.Parent.Activated:Connect(function()
	if db == false then
		db = true
		anim:Play()
		new:HitStart()
		wait(1)
		new:HitStop()
		wait(1)
		db = false
	end
end)
1 Like

Did you try debugging? (Just put one print after defining all the variables, one in the .OnHit block of code, and one in the .Activated block of code, and see what prints and what doesn’t.)

Going off what @MasterMinionnn said. It looks like the issue could be due to somthing outside this code.
I suspect it’s somthing related to all the variables at the top. At some points you are “wait()”-ing. And the code could be hanging on one spot because a condition hasn’t been met for some reason. Like if the script is in the wrong place.

One thing that could make it more clear is to clean up the variables a bit:

local rs = game:GetService("ReplicatedStorage")
local rc = require(rs.Modules.RaycastHitboxV4)

local tool = script.Parent
local humanoid = nil

local AnimFolder = tool.Anims
local slash = humanoid:LoadAnimation(AnimFolder.Slash) -- yes, these lines will have issues. Just keep reading!
local slash2 = humanoid:LoadAnimation(AnimFolder.Slash2)
local slash3 = humanoid:LoadAnimation(AnimFolder.Slash3)
local randomAnim = {slash, slash2, slash3}
local anim = randomAnim[math.random(1, #randomAnim)]

local db = false
local new = rc.new(tool.Handle)

--repeat wait() until script.Parent.Parent:WaitForChild("Humanoid")
--local hum = script.Parent.Parent:WaitForChild("Humanoid")

======== Update! ========

I may have also found your issue while going through and tidying the variables. Goes to show you that clean and easy to read code goes a long way.

But it looks like what may have been happening is that the script inside the tool will execute immediately when the player spawns. But the player isn’t holding it. So in that case, script.Parent.Parent would not be the player’s model as you expect when the player equips the tool.
The problem was that you were assuming that the script started executing when the tool was equipped. But it’s when the tool was created inside the player’s backpack.

Therefore, this one line basically ran forever, and never stopped.
repeat wait() until script.Parent.Parent:WaitForChild("Humanoid")

What you should do, is have any code that references the player’s model be inside an event that fires when the tool is equipped… Now I almost mistakenly said that would be “Activated” (though you could do that). But it would be better to do it in the “equipped” event.

tool.Equipped:Connect(function()
	-- This will assign a value to the variable we created above
	-- Note, I changed the variable name from "hum" to "humanoid" because it's worth spelling it all out.
	humanoid = script.Parent.Parent.Humanoid

	local slash = humanoid:LoadAnimation(AnimFolder.Slash)
	local slash2 = humanoid:LoadAnimation(AnimFolder.Slash2)
	local slash3 = humanoid:LoadAnimation(AnimFolder.Slash3)
	randomAnim = {slash, slash2, slash3}
	anim = randomAnim[math.random(1, #randomAnim)]
end)

To be 100% clear, the “Activated” event fires when you press left click with the tools equipped, and “Equipped” happens whent the tool is equipped.

I should finally note that some of the variables have now been moved into the Equipped event in the second code snippet. So your variables at the top would look more like:

local rs = game:GetService("ReplicatedStorage")
local rc = require(rs.Modules.RaycastHitboxV4)

local tool = script.Parent
local humanoid = nil

local AnimFolder = tool.Anims
local randomAnim = nil
local anim = nil

local db = false
local new = rc.new(tool.Handle)

local Params = RaycastParams.new()
Params.FilterType = Enum.RaycastFilterType.Blacklist

new.RaycastParams = Params

tool.Equipped:Connect(function()
	-- This will assign a value to the variable we created above
	-- Note, I changed the variable name from "hum" to "humanoid" because it's worth spelling it all out.
	humanoid = script.Parent.Parent.Humanoid

	local slash = humanoid:LoadAnimation(AnimFolder.Slash)
	local slash2 = humanoid:LoadAnimation(AnimFolder.Slash2)
	local slash3 = humanoid:LoadAnimation(AnimFolder.Slash3)
	randomAnim = {slash, slash2, slash3}
	anim = randomAnim[math.random(1, #randomAnim)]

	Params.FilterDescendantsInstances = {humanoid.Parent} --- remember to define our character!
end)

-- I am deliberately ignoring "new.OnHit" here, but it would be here.

tool.Activated:Connect(function()
	-- This is just a safety check to make sure everything is working as expected
	-- You don't have to call "error" here, you could just "print" a message to inform you that there's an issue.
	if humanoid == nil then
		error("Humanoid Reference Was Not Found!")
		return
	end

	if db == false then
		db = true
		anim:Play() -- We are doing the safety check above, because of humanoid is nil, then this is nil as well.
		new:HitStart()
		wait(1)
		new:HitStop()
		wait(1)
		db = false
	end
end)

The reason the “slash”, “slash2”, “slash3” variables are no longer there, is because the code has no idea about the player’s humanoid when that code runs (right when the player first spawns, not holding any tools). So they cannot be there, they have to be in Equipped. Or in some other code that happens when the tool is parented under the player’s Model.

Last thing I will say is that, I have not tested any of this code. So there could be a different issue, but it’s likely it will be easier to fix. Like it may be a typo I made somwhere.
But the core of your issue (I believe) was just that the script never found it’s humanoid, because it was checking for it too early. It should happen in “Equipped”.


Also, it looks like “humanoid:LoadAnimation” is deprecated.
So it’s reccomended to use “Animator:LoadAnimation()” instead. You are free to continue using the humanoid version. But I’ll let you know that this exists if the animator gives you other issues, that could be related to it being an obsolete API; things might be better in the new one. (I woudn’t know I’ve never used it)
Humanoid | Roblox Creator Documentation
Animator | Roblox Creator Documentation

2 Likes

thank youi will gonna try that

Hey i did not try script i gonna try it now but when i test the game in the output it says httpError:ConnectError() is it not sending the tool data to server?

That is not related to this script. It’s either another script that uses HttpService, or a plugin that you have that is causing the error.

So was that related to the script.Parent.Parent:WaitForChild("Humanoid") ?

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