If else hit function

I’m trying to make a dash hit and I’m using hit functions from other people’s topics but when I try using the hit function and something is wrong with the if-else statement:

local cool = false
		player.Character:WaitForChild("HitBox").Touched:Connect(function(hit)
			local target = hit.Parent:FindFirstChild("HumanoidRootPart")
			if hit and target then
				if cool == false then
				cool = true
					print(target.Parent.Name)
				wait(.2)
					cutscene2(player)
					recieveTarget:FireClient(player)
						
					target.Parent:WaitForChild("HumanoidRootPart").CFrame = player.Character:WaitForChild("HumanoidRootPart").CFrame * CFrame.new(0,4,9)
				
				    local constant = Instance.new("WeldConstraint",player.Character:WaitForChild("HumanoidRootPart"))
				    local mainModel = game.ReplicatedStorage.VfxFolder:WaitForChild("slashcopy"):Clone()
				    local mainPart = mainModel.centralPoint
				    local rotation = CFrame.Angles(0,0,math.rad(40))
				    local modelcframe = target.Parent:GetPivot()

				    mainModel.Parent = target.Parent
				    mainModel:PivotTo(modelcframe * rotation)
				    mainModel.PrimaryPart = target.Parent:WaitForChild("HumanoidRootPart")
				    constant.Part0 = target.Parent:WaitForChild("HumanoidRootPart")
				    constant.Part1 = mainPart

				    gui(player)
				    wait(1)
				    player.Character:WaitForChild("HitBox"):Remove()
					cool = false
					
				elseif hit and target and cool == false then
					
				    cool = true
				    missed(player)
				    print("missed")
				    wait(2)
				    cool = false
				    player.Character:WaitForChild("HitBox"):Remove()
			end		
		end
	end)

I wanted to make when the player dashes and if the hitbox finds the target cutscene will play with the target but if not then some function will play but the problem is that hitbox will spawn at the player HumanoidRootPart and the hitbox finds the player HumanoidRootPart not the target’s HumanoidRootPart This is a list of the problems that I tried to fix:

  1. If I hit the target both if statements trigger
  2. if statements don’t work

if you are confused that’s normal I can’t even describe with my bad English

I’m quite confused as to what your exact issue is, Could you add the error message that you’re receiving?

There is no error just does not work as it should be and if it does work then not properly!

  • How does need to work

When the button is pressed I already do write that player dashes when the player dashes The problem: comes the function that if the hitbox finds the target then the first if need to be played on the target NOT THE SECOND IF STATEMENT and if not find the target then the second if need to be played on the player

  • What is the actual problem

When I start the function the player dashes I already write that And if the hitbox doesn’t find the target then the first if is triggered not the second when I’m not finding the target but it triggers on the player it is supposed to be on the target if there is a target and the second if it’s not working if there is no target

so basically first if is triggered on the player and the second if does not work!

I don’t really know how I can help here, Sorry.

1 Like

Read your script again and then see what’s wrong:

if hit and target then
    if cool == false then

    else if hit and target and cool == false then

What do you think your issue is here?

Your code has too many flaws, I rewrote it for ya. I don’t even know what you want to achieve, so this probably won’t work out of the blue for you.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- You may want to declare services as variables.

local cooldown = false
local Character = player.Character
local HitBox = Character:WaitForChild("HitBox")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
-- Please use variables.

HitBox.Touched:Connect(function(hitPart)
    if cooldown then return end -- If the cooldown is true, then we don't want to do anything.
    local targetHumanoidRootPart = hitPart.Parent:FindFirstChild("HumanoidRootPart") 
    if not targetHumanoidRootPart then
        cooldown = true
        missed(player)
        task.wait(2)
        cooldown = false
        return
    end
    -- If the target doesn't have a HumanoidRootPart, we want to run the missed function and return out of the function.
    -- This way you don't have to wrap the rest of the code in an if statement.

    cooldown = true

    -- let's declare variables first
    local WeldConstraint = Instance.new("WeldConstraint") -- do NOT use the second argument for parenting. 
    local mainModel = ReplicatedStorage.VfxFolder:WaitForChild("slashcopy"):Clone()
    local mainPart = mainModel.centralPoint

    task.wait() -- Please use task.wait() instead of wait(). In fact, do you even need to wait here?
    cutscene2(player)
    recieveTarget:FireClient(player)
    targetHumanoidRootPart.CFrame = HumanoidRootPart.CFrame * CFrame.new(0,4,9)

    
    WeldConstraint.Part0 = targetHumanoidRootPart
    WeldConstraint.Part1 = mainPart
    WeldConstraint.Parent = HumanoidRootPart

    mainModel.Parent = targetHumanoidRootPart.Parent
    mainModel:PivotTo(
        targetHumanoidRootPart:GetPivot() *
        CFrame.Angles(0, 0, math.rad(40))
    )
    mainModel.PrimaryPart = targetHumanoidRootPart

    gui(player)
    task.wait(1) -- again, please use task.wait() instead of wait() - if you really need to wait here.

    mainModel:Destroy()
    WeldConstraint:Destroy()
    -- Use :Destroy() instead of :Remove(). This is not even a recommendation, this is a requirement.
    -- Also, why would you want to destroy the Hitbox? This won't work after the first time you use it.

    cooldown = false -- this is the last thing we want to do
end)
1 Like

Thanks for the help

I just have many problems when I try to fix, so I just pointed at one problem, but I forgot about the logic, instead of describing one problem and then trying to fix it. Avoiding confusion and waste of time!

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