Hi! I’m making a script where if a character with a humanoid gets hit, they take damage, knockback etc. The problem is that sometimes it gets hit and sometimes it doesn’t. Sometimes it even takes 5 whole minutes for me to be able to hit the character. When I don’t hit the character, it just prints out “nil”.
Script:
local touched = false
local TS = game:GetService("TweenService")
game.ReplicatedStorage.Throw.OnServerEvent:Connect(function(plr, mouseHit)
plr.Character.Humanoid:LoadAnimation(script.Parent.Handle:FindFirstChild("Yeet")):Play()
local clone = plr.Character:FindFirstChildWhichIsA("Tool").Handle:Clone()
clone.CanTouch = true
clone.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= plr.Character and hit.Parent.Name ~= "Rope" then
if not touched then
local target = hit.Parent
local joints = target:GetDescendants()
target.Humanoid.BreakJointsOnDeath = false
touched = true
clone.CanTouch = false
target.HumanoidRootPart.Velocity = plr.Character.HumanoidRootPart.CFrame.LookVector * 160 + Vector3.new(0,160,0)
clone.Impact:Play()
print(hit.Parent.Name,"was hit by",script.Parent.Name)
hit.Parent:FindFirstChild("Humanoid").Health -= 10
for _,joint in pairs(joints) do
if joint:isA("Motor6D") then
local socket = Instance.new("BallSocketConstraint")
local att0 = Instance.new("Attachment")
local att1 = Instance.new("Attachment")
print(joint)
target.Humanoid:ChangeState(Enum.HumanoidStateType.Ragdoll)
target.Humanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp, false)
target.Humanoid.AutoRotate = false
--target.Humanoid.PlatformStand = true
--target.Humanoid.WalkSpeed = 0
--target.Humanoid.JumpPower = 0
att0.Name = "Att0"
att1.Name = "Att1"
att0.CFrame = joint.C0
att1.CFrame = joint.C1
att0.Parent = joint.Part0
att1.Parent = joint.Part1
touched = true
clone.CanTouch = false
socket.Parent = joint.Part0
socket.Attachment0 = att0
socket.Attachment1 = att1
att0.Name = "Att0"
att1.Name = "Att1"
socket.LimitsEnabled = true
socket.TwistLimitsEnabled = true
--wait(0.2)
--wait(2)
joint.Enabled = false
end
end
if target.Humanoid.Health > 0 then
if target.Name ~= "Dummy3" then
wait(0.2)
clone:Destroy()
touched = false
clone.CanTouch = true
wait(1)
target.Humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
target.Humanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp, true)
target.Humanoid.AutoRotate = true
target.Humanoid.WalkSpeed = 26
target.Humanoid.JumpPower = 7.5
for _,joint2nd in pairs(joints) do
if joint2nd:isA("Motor6D") then
joint2nd.Enabled = true
end
end
for _, child in pairs(target:GetDescendants()) do
if child.Name == "Att0" or child.Name == "Att1" or child:IsA("BallSocketConstraint") then
child:Destroy()
end
end
end
end
end
end
end)
wait(0.4)
plr.Character:FindFirstChildWhichIsA("Tool").Handle.Woosh:Play()
wait(0.1)
plr.Character:FindFirstChildWhichIsA("Tool").Handle.Transparency = 1
print(clone.Parent)
clone.Parent = game.Workspace
-- clone.Position = script.Parent.Handle.Position
clone.Anchored = true
clone.CanTouch = true
clone.CanCollide = false
clone.CFrame = plr.Character.HumanoidRootPart.CFrame+plr.Character.HumanoidRootPart.CFrame.LookVector * 5
local Tween = TS:Create(clone,TweenInfo.new(0.5),{CFrame = plr.Character.HumanoidRootPart.CFrame + plr.Character.HumanoidRootPart.CFrame.LookVector * 80})
Tween:Play()
--print(mouseHit)
wait(0.45)
plr.Character:FindFirstChildWhichIsA("Tool").Handle.Transparency = 0
clone:Destroy()
end)
--clone.Touched:Connect(function(hit)
--local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
--print(plr,"was hit by",script.Parent.Name)
--end)
local touched = false
local TS = game:GetService("TweenService")
game.ReplicatedStorage.Throw.OnServerEvent:Connect(function(plr, mouseHit)
local clone = plr.Character:FindFirstChildWhichIsA("Tool").Handle:Clone()
clone.Parent = game.Workspace
clone.Anchored = true
clone.CanCollide = false
clone.CanTouch = true
clone.Transparency = 1
plr.Character.Humanoid:LoadAnimation(script.Parent.Handle:FindFirstChild("Yeet")):Play()
clone.CanTouch = true
clone.Touched:Connect(function(hit)
print(hit.Parent)
if hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= plr.Character and hit.Parent.Name ~= "Rope" then
if not touched then
local target = hit.Parent
local joints = target:GetDescendants()
target.Humanoid.BreakJointsOnDeath = false
touched = true
clone.CanTouch = false
target.HumanoidRootPart.Velocity = plr.Character.HumanoidRootPart.CFrame.LookVector * 160 + Vector3.new(0,160,0)
clone.Impact:Play()
print(hit.Parent.Name,"was hit by",script.Parent.Name)
hit.Parent:FindFirstChild("Humanoid").Health -= 10
for _,joint in pairs(joints) do
if joint:isA("Motor6D") then
local socket = Instance.new("BallSocketConstraint")
local att0 = Instance.new("Attachment")
local att1 = Instance.new("Attachment")
print(joint)
target.Humanoid:ChangeState(Enum.HumanoidStateType.Ragdoll)
target.Humanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp, false)
target.Humanoid.AutoRotate = false
--target.Humanoid.PlatformStand = true
--target.Humanoid.WalkSpeed = 0
--target.Humanoid.JumpPower = 0
att0.Name = "Att0"
att1.Name = "Att1"
att0.CFrame = joint.C0
att1.CFrame = joint.C1
att0.Parent = joint.Part0
att1.Parent = joint.Part1
touched = true
clone.CanTouch = false
socket.Parent = joint.Part0
socket.Attachment0 = att0
socket.Attachment1 = att1
att0.Name = "Att0"
att1.Name = "Att1"
socket.LimitsEnabled = true
socket.TwistLimitsEnabled = true
--wait(0.2)
--wait(2)
joint.Enabled = false
end
end
if target.Humanoid.Health > 0 then
if target.Name ~= "Dummy3" then
wait(0.2)
clone:Destroy()
touched = false
clone.CanTouch = true
wait(1)
target.Humanoid:ChangeState(Enum.HumanoidStateType.GettingUp)
target.Humanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp, true)
target.Humanoid.AutoRotate = true
target.Humanoid.WalkSpeed = 26
target.Humanoid.JumpPower = 7.5
for _,joint2nd in pairs(joints) do
if joint2nd:isA("Motor6D") then
joint2nd.Enabled = true
end
end
for _, child in pairs(target:GetDescendants()) do
if child.Name == "Att0" or child.Name == "Att1" or child:IsA("BallSocketConstraint") then
child:Destroy()
end
end
end
end
end
end
end)
wait(0.4)
plr.Character:FindFirstChildWhichIsA("Tool").Handle.Woosh:Play()
wait(0.1)
plr.Character:FindFirstChildWhichIsA("Tool").Handle.Transparency = 1
clone.Transparency = 0
print(clone.Parent)
-- clone.Position = script.Parent.Handle.Position
clone.CFrame = plr.Character.HumanoidRootPart.CFrame+plr.Character.HumanoidRootPart.CFrame.LookVector * 5
local Tween = TS:Create(clone,TweenInfo.new(0.5),{CFrame = plr.Character.HumanoidRootPart.CFrame + plr.Character.HumanoidRootPart.CFrame.LookVector * 80})
Tween:Play()
--print(mouseHit)
wait(0.45)
plr.Character:FindFirstChildWhichIsA("Tool").Handle.Transparency = 0
clone:Destroy()
end)
--clone.Touched:Connect(function(hit)
--local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
--print(plr,"was hit by",script.Parent.Name)
--end)
This is the code that’s wrong:
clone.Touched:Connect(function(hit)
print(hit.Parent)
if hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= plr.Character and hit.Parent.Name ~= "Rope" then
It sometimes touches the player and then it quits the if statement because the player getting hit was the player who fired the code. If there was a way to fire the if statement multiple times. I don’t know what to do. Would you use repeat until?
Add a hitbox to the tool (ex: a part around the blade of a sword) and do clone.Hitbox.Touched:Connect(etc) so it will only detect if something touches the blade, not the entire tool (which the player is holding)
I know, but like… if the handle is the entire tool, then obviously it would hit the player holding the tool when the player holding the tool fires the event because the player is touching the handle. What I mean is detecting whether a specific part (not the part the player is holding) of the tool is touched.