Part.Touched function help!

I really don’t know exactly how to begin asking this but basically I have a bat that when swung it will make a swish sound. But if the bat was to hit anything other than the player holding the bat it would make a “thud” sound. you know what i mean. Basic stuff yeah. My question is, how would one go about to implement it into this series of code. I have an idea of what it could look like but don’t have the scripting knowledge to complete it. PS. “HitBox” is the Bat… Technically. I just didn’t want the bat’s collision to interfere in any way so i have a separate part.

local Player = game:GetService("Players").LocalPlayer
local char = Player.Character or Player.CharacterAdded:Wait()
local h = char:WaitForChild("Humanoid")
local Idle = h:LoadAnimation(script.Anims:WaitForChild("Idle"))
local Swing = h:LoadAnimation(script.Anims:WaitForChild("Swing"))
local Hit = h:LoadAnimation(script.Anims:WaitForChild("Hit"))
local Tool = script.Parent.Parent
local HitBox = script.Parent.HitBox

Tool.Equipped:Connect(function(player)

	Idle:Play()

end)

Tool.Unequipped:Connect(function(player)

	Idle:Stop()
end)

Tool.Activated:Connect(function()
	
	if HitBox.Touched then
		script.Parent.Hit:Play()

-- Here is where i presume the "Touch" code could be right?
	end
	
	Idle:Stop()
	script.Parent.Swing:Play()
	Swing:Play()
	print("Swung Bat")
	Idle:Play()
end)

Any help or advice or even a solution is greatly appreciated. My coding knowledge is still a bit primitive so i apologize if my code is cringy or something xD.

you could check the parent of what the hitbox touches, and if its anything but the player, or players you’re hitting it with, then you can play the thud sound, i believe u can use “hit” and apply inside a .Touched event function where ur “if HitBox” is and delete that n replace it, what u can do is u can say oh, HitBox.Touched:Connect(function(hit) and so on id reckon and check if the hit.parent is different than what i described in the beginning n go on ur way, if u need more clarification u can say so

1 Like

Oh thank you! sorry for a late responce, i was just editing it and got side tracked. It works! however I have one more small request of you, if thats okay. Is there a way to check if the touched part (the bat hitting the target) has hit something with a Humanoid? I know its inside of the hit.touched:connect function but maybe you know how to call for its humanoid?

sorry im so used to doing coding that was for atmosphere. Doors opening and closing, lights, music etc xD but not combat stuff.

You could use Instance:FindFirstChildWhichIsA(className: string, recursive: boolean?). If you know or only care that the Humanoid is on the first level, you can avoid setting recursive to true or use Instance:FindFirstChildOfClass(className: string).

EDIT: If the reply from @nurtureneko solved your problem, please mark it as the solution!

EDIT2: I just realised you’ll probably receive parts of the assembly, and not the model itself, in which case you can use the ancestry function Instance:FindFirstAncestorOfClass(className: string) to find the Model and then use the child-finding methods on that to look for a Humanoid.

yea you could do what he said or, just use the hit.Parent and use this, make sure its put inside of your .Touched function, if it doesnt work u could try his methods as well!

if hit.Parent:FindFirstChild("Humanoid") then 

--code

else

--code

end
1 Like

oh god yes that makes so much sense. Im so sorry lol but thank you! yes yes it works perfect now! :smiley: Thank you!! :smiley:

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