So I made a tool that whenever someone hits their tool to someone else, they will activate the sit function. I tried doing this but it didn’t seem to work. Note: Currently it works with animations.
Here is a better understanding of what I want my tool to do:
So the red is the pool noodle. Since it’s in its idle phase, I don’t want to activate it
But whenever the animation plays I want it so whenever the pool noodle touches the humanoid, it activates the sit function. It doesn’t work. I tried turning off CanCollide on the pool noodle and nothing changed
Here is the script as well if your wondering:
local char
local humanoid
local tool = script.Parent
local findHumanoid = game.Workspace
local motor6D
local connection
local touched = false
local animSet = 1
local idleAnim
local hitAnim1
local hitAnim2
local hitAnim3
local hit = tool.BodyAttach.Hit
local swing = tool.BodyAttach.Swing
function OnTouched(part)
if touched then
return
end
touched = true
local humanoid = findHumanoid:FindFirstChild("Humanoid")
if humanoid ~= nil then
hit:Play()
humanoid.Sit = true
wait(1)
humanoid.Sit = false
end
touched = false
end
tool.Equipped:Connect(function()
char = tool.Parent
humanoid = tool.Parent:FindFirstChild("Humanoid")
if humanoid ~= nil then
idleAnim = humanoid:LoadAnimation(script.IdleAnim)
hitAnim1 = humanoid:LoadAnimation(script.HitAnim1)
hitAnim2 = humanoid:LoadAnimation(script.HitAnim2)
hitAnim3 = humanoid:LoadAnimation(script.HitAnim3)
end
motor6D = Instance.new("Motor6D")
motor6D.Name = "ToolGrip"
motor6D.Parent = char.UpperTorso
char.UpperTorso.ToolGrip.Part0 = char.UpperTorso
char.UpperTorso.ToolGrip.Part1 = tool.BodyAttach
idleAnim:Play()
end)
tool.Unequipped:Connect(function()
motor6D:Destroy()
idleAnim:Stop()
animSet = 1
end)
tool.Activated:Connect(function()
if tool.Enabled == true then
if animSet == 1 then
hitAnim1:Play()
animSet = 2
elseif animSet == 2 then
hitAnim2:Play()
animSet = 3
elseif animSet == 3 then
hitAnim3:Play()
animSet = 1
end
swing:Play()
connection = tool.BodyAttach.Touched:Connect(OnTouched)
end
tool.Enabled = false
wait(2) -- reload speed
connection:Disconnect()
tool.Enabled = true
end)
Ok nevermind, I was confused for a sec, though how would I be able to check if the humanoid is the same player that equipped the tool. I want it to only work for other players
I don’t quite understand, but the problem is that the humanoid variable, is nil. Unless you have a random humanoid in the workspace, the if statement won’t work, because it can’t find a Humanoid. Which is why you need to search from the part you hit. In the OnTouched function, roblox gives you the pre-set instance of what touched the part. And you named that “part”. We need to find the parent of the part, and then check for a humanoid. What you’re doing, is just going to the workspace, and looking for a random humanoid, which as I said, does not work.
You need to do:
local humanoid = part.Parent:FindFirstChild("Humanoid")
if humanoid ~= nil then
hit:Play()
humanoid.Sit = true
wait(1)
humanoid.Sit = false
end
Adding on, you can just compare the Humanoid of the other player and check if it’s ~= (not equal to) the player who has the item. The tool, when equipped is placed as a child of the character, so you can just find the parent of the tool, then find the Humanoid, and compare.
I also recommend moving the line:
to make it on the last line.
Because every time you activate the tool, it connects the function, again and again, if it happens enough times, it will cause your game lag, which is, not gud.
if humanoid ~= nil and Tool.Parent:FindFirstChild("Humanoid") ~= humanoid then --Checks to make sure they're not the same humanoid.
hit:Play()
humanoid.Sit = true
wait(1)
humanoid.Sit = false
end