Need help on my Combat System | ASAP

So I am working on a Combat system, and I want Mouse 1 and Mouse 2 click to have attacking abillity, like when you click Left Mouse It should attack ( Which does ) When you click Right Mouse button It should also attack but It doesn’t does that,I did It on Mouse 1 but I don’t know what Is wrong with Mouse 2, I have tried same script to make Mouse 2 work, but It didn’t work… then I tried changing the whole script but nope It didn’t work again. Btw I am trying to make them work as a Tool, I am doing all work In a Tool object, I thought It would be easier…

Screenshots

Script
Tool = script.Parent
Handle = Tool:WaitForChild("Handle")

Players = game:GetService("Players")
RunService = game:GetService("RunService")
ContextActionService = game:GetService("ContextActionService")
UserInputService = game:GetService("UserInputService")

Animations = {}
LocalObjects = {}

ServerControl = Tool:WaitForChild("ServerControl")
ClientControl = Tool:WaitForChild("ClientControl")

ToolEquipped = false

function SetAnimation(mode, value)
	if mode == "PlayAnimation" and value and ToolEquipped and Humanoid then
		local Edited = false
		for i, v in pairs(Animations) do
			if v.Animation == value.Animation then
				v.AnimationTrack:AdjustSpeed(value.Speed)
				--v.AnimationTrack:AdjustWeight(value.Weight, value.FadeTime)
				Edited = true
				break
			end
		end
		if Edited then
			return
		end
		local AnimationTrack = Humanoid:LoadAnimation(value.Animation)
		table.insert(Animations, {Animation = value.Animation, AnimationTrack = AnimationTrack})
		AnimationTrack:Play(value.FadeTime, value.Weight, value.Speed)
	elseif mode == "StopAnimation" and value then
		for i, v in pairs(Animations) do
			if v.Animation == value.Animation then
				v.AnimationTrack:Stop(value.FadeTime)
				table.remove(Animations, i)
			end
		end
	end
end


function DisableJump(Boolean)
	if PreventJump then
		PreventJump:disconnect()
	end
	if Boolean then
		PreventJump = Humanoid.Changed:connect(function(Property)
			if Property ==  "Jump" then
				Humanoid.Jump = false
			end
		end)
	end
end

function CheckIfAlive()
	return (((Character and Character.Parent and Humanoid and Humanoid.Parent and Humanoid.Health > 0 and Player and Player.Parent) and true) or false)
end

function Equipped(Mouse)
	Character = Tool.Parent
	Player = Players:GetPlayerFromCharacter(Character)
	Humanoid = Character:FindFirstChild("Humanoid")
	ToolEquipped = true
	if not CheckIfAlive() then
		return
	end
	PlayerMouse = Player:GetMouse()
	Mouse.Button1Down:connect(function()
		InvokeServer("Button1Click", {Down = true})
	end)
	Mouse.Button1Up:connect(function()
		InvokeServer("Button1Click", {Down = false})
	end)
	Mouse.KeyDown:connect(function(Key)
		InvokeServer("KeyPress", {Key = Key, Down = true})
	end)
	Mouse.KeyUp:connect(function(Key)
		InvokeServer("KeyPress", {Key = Key, Down = false})
	end)
	if UserInputService.TouchEnabled then
		ContextActionService:BindActionToInputTypes("Special", (function()
			InvokeServer("KeyPress", {Key = "q", Down = true})
		end), true, "")
		ContextActionService:SetTitle("Special", "Special")
	end
end

function Unequipped()
	ToolEquipped = false
	LocalObjects = {}
	if UserInputService.TouchEnabled then
		ContextActionService:UnbindAction("Special")
	end
	for i, v in pairs(Animations) do
		if v and v.AnimationTrack then
			v.AnimationTrack:Stop()
		end
	end
	for i, v in pairs({PreventJump, ObjectLocalTransparencyModifier}) do
		if v then
			v:disconnect()
		end
	end
	Animations = {}
end

function InvokeServer(mode, value)
	local ServerReturn
	pcall(function()
		ServerReturn = ServerControl:InvokeServer(mode, value)
	end)
	return ServerReturn
end

function OnClientInvoke(mode, value)
	if mode == "PlayAnimation" and value and ToolEquipped and Humanoid then
		SetAnimation("PlayAnimation", value)
	elseif mode == "StopAnimation" and value then
		SetAnimation("StopAnimation", value)
	elseif mode == "PlaySound" and value then
		value:Play()
	elseif mode == "StopSound" and value then
		value:Stop()
	elseif mode == "MousePosition" then
		return {Position = PlayerMouse.Hit.p, Target = PlayerMouse.Target}
	elseif mode == "DisableJump" then
		DisableJump(value)
	elseif mode == "SetLocalTransparencyModifier" and value and ToolEquipped then
		pcall(function()
			local ObjectFound = false
			for i, v in pairs(LocalObjects) do
				if v == value then
					ObjectFound = true
				end
			end
			if not ObjectFound then
				table.insert(LocalObjects, value)
				if ObjectLocalTransparencyModifier then
					ObjectLocalTransparencyModifier:disconnect()
				end
				ObjectLocalTransparencyModifier = RunService.RenderStepped:connect(function()
					for i, v in pairs(LocalObjects) do
						if v.Object and v.Object.Parent then
							local CurrentTransparency = v.Object.LocalTransparencyModifier
							if ((not v.AutoUpdate and (CurrentTransparency == 1 or  CurrentTransparency == 0)) or v.AutoUpdate) then
								v.Object.LocalTransparencyModifier = v.Transparency
							end
						else
							table.remove(LocalObjects, i)
						end
					end
				end)
			end
		end)
	end
end

ClientControl.OnClientInvoke = OnClientInvoke
Tool.Equipped:connect(Equipped)
Tool.Unequipped:connect(Unequipped)
4 Likes

Where is your code for Mouse.Button2Down:Connect? I can only see functions for Mouse.Button1Down & Mouse.Button1Up.
Also, please make sure you use capitals in your :Connect

Sorry, I don’t have discord.

Post must be at least E0 characters

If your current functions for MouseButton1 work, then just clone them for MouseButton2 within the same script. They take the same Connects as MouseButton1:

I couldn’t find how to use the script you sent…

Have you perhaps made an attempt on this but alternatively using UserInputService?

1 Like

No I haven’t, Mr. xxIamInevitable.

You should. For example:

for right click:

local uis = game:GetService("UserInputService");

uis.InputBegan:Connect(function(inp, gpe) --mousebutton2 is more than likely a gpe, so ignore gpe here
    if inp.KeyCode == Enum.KeyCode.MouseButton2 then --if the input is right-click, then
        -- do stuff
    end
end

for left click, it’s just mousebutton 1

2 Likes

Where should I put the script? btw thanks

the same script you had the main script on

2 Likes