Trying to make a very simple sword combat

I’m trying to figure out how to make a sword play another animation if m1/left click is pressed again, and again. Essentially a combo.

This is the code I’m working with. I’m using swordphin’s public raycast module to help with it.

local userInputService = game:GetService("UserInputService")

local tool = script.Parent

local player = game:GetService("Players").LocalPlayer
local characterPlayer = player.Character or player.CharacterAdded:Wait()
local humanoidPlayer = characterPlayer:WaitForChild("Humanoid")


local replicatedStorage = game.ReplicatedStorage
local raycastModule = require(replicatedStorage.RaycastHitboxV4)
local hitBox = raycastModule.new(tool)

--local newHitbox = raycastModule.new(script.Parent.Hitbox)

hitBox:SetPoints(tool.Handle, {Vector3.new(1, 0, 0), Vector3.new(5, 0, 0), Vector3.new(10, 0, 0)})

--newHitbox:SetPoints(script.Parent, {Vector3.new(0, 3, 0), Vector3.new(-5, 3, 0), Vector3.new(5, 3, 0)})

local RaycastParams = RaycastParams.new()
RaycastParams.FilterDescendantsInstances = {characterPlayer}
RaycastParams.FilterType = Enum.RaycastFilterType.Exclude

hitBox.RaycastParams = RaycastParams

function OnTouch(hit, humanoid)
	print(hit.Name)
	humanoid:TakeDamage(25)
end

function OnActivation()
	hitBox:HitStart() --- Starts the hit detection
	wait(10) --- Wait a few seconds
	hitBox:HitStop() --- Stops the hit detection
end

hitBox.OnHit:Connect(OnTouch)
tool.Activated:Connect(OnActivation)

local animation = Instance.new("Animation")

animation.AnimationId = "rbxassetid://15161132977"

local track

local function onInputBegan(input, _gameProcessed)
	if input.UserInputType == Enum.UserInputType.MouseButton1 then
		track = characterPlayer.Humanoid.Animator:LoadAnimation(animation)
		track.Priority = Enum.AnimationPriority.Action4
		track.Looped = false
		track:Play()
		wait(10)
		track:Stop()
	end
end

userInputService.InputBegan:Connect(onInputBegan)

--userInputService.InputEnded:Connect(function(input, gameProcessed)
--	if input.UserInputType == Enum.UserInputType.MouseButton1 then
--		wait(2)
--		track:Stop()
--	end
--end)

I’m a beginner so this might not be the best first project I should be doing.

Create a currentPunch variable initially set to 1
Within your onInputBegan function, after the check for left-click, handle currentPunch to manage different animations based on the number of clicks.
Once currentPunch reaches 5, you should reset it to 1. You would also need to use os.clock or something like that to check if the player hasnt m1d in a x amount of time, and if they havent then put the combo back to 1 if that makes sense
You can also use tables instead of checking if its at each singular value but this is more beginner friendly.

Create two variables, one storing the current attack number and a newproxy() value. Whenever you do an attack, increase that attack number by 1, and overwrite that proxy with a new value. After a certain interval, check to see if the proxy has been overwritten again. If not, set m1s back to 1.

newproxy() returns an empty value that is completely unique, which is useful for making sure that nothing new has happened.

CurrentM1Attack = 1
CurrentM1Proxy = newproxy()

MaxM1Attacks = 5 --// set this to whatever you want the combo ender to be.
M1_Interval = 0.5 --// how long to wait before reseting an M1

function StartM1Attack()
    if CurrentM1Attack <= MaxM1Attacks then 
        DoSwordAnimation(CurrentM1Attack) --//Attack stuff here. 

        CurrentM1Attack += 1 --// Increase M1s by 1
        local RunProxy = newproxy() --// Overwrite old proxy.
        CurrentM1Proxy = RunProxy --// Set global proxy to new proxy for later comparison.
        task.wait(M1_Interval)

        if RunProxy == CurrentM1Proxy then --// Check if the global proxy hasn't been overwritten.
            CurrentM1Attack = 1 --// Reset M1s to zero.
        end
    end
end

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