Solved solved solved solved solved

solved solved solved solved solved solved

I believe it might be because on the local script I use tool.equipped and tool.unequipped differently than on the server script, but I know animations replicate to server automatically. So it might not be the issue.

Hmm, make sure to disconnect your connections, similar problems to this other post.

Even unequipped the local script should still be running in your backpack and hence still be listening to input connections.

Like so:

tool.Equipped:Connect(function()
	connection = uis.InputBegan:Connect(function(key,processed)
end)
end)


tool.Unequipped:Connect(function()
	track.Looped = false
	track:Stop()
if connection  then
connection :Disconnect()
connection  = nil
end
end)

Also recommend CAS context action service, as you don’t have to deal with connections just binding and unbinding of action names stuff.

1 Like

You shouldn’t have the animation played on both the server and the client. The client has control over their own character’s physics, so any animation played on the client’s character will be replicated to other clients. You should only be loading the animation in the local script instead of both the local script and the server script.

Also, you should use ContextActionService like @dthecoolest mentioned, as it is easier to work with and versatile than having a bunch of event connections. Ideally, with ContextActionService, you should be able to set up a main framework for reacting to client input in a single local script and then have different modules for each tool.

I never worked with CAS, so that’s why I didnt use it here. Never really understood the tutorial on it on the hub. What does it do? / contribute?

Instead of 2 connections, you can have 1 CAS binding since the keycode is the same left shift.

	uis.InputBegan:Connect(function(key,processed)
		if processed then return end
		if key.KeyCode == Enum.KeyCode.LeftShift then
			char:WaitForChild("Humanoid").WalkSpeed = 25
			track = char.Humanoid:LoadAnimation(script.Parent.Return.Running)
			track.Priority = Enum.AnimationPriority.Idle
			track.Looped = true
			track:Play()
		end
	end)
	uis.InputEnded:Connect(function(key)
		if key.KeyCode == Enum.KeyCode.LeftShift then
			char:WaitForChild("Humanoid").WalkSpeed = 16
			track.Looped = false
			track:Stop()
		end
	end)

CAS handles it all in one function, code

local function handleAction(actionName, inputState, inputObject)
	if actionName == "Sprint" then
		if inputState == Enum.UserInputState.Begin then
			char:WaitForChild("Humanoid").WalkSpeed = 25
			track = char.Humanoid:LoadAnimation(script.Parent.Return.Running)
			track.Priority = Enum.AnimationPriority.Idle
			track.Looped = true
			track:Play()
		end

		if inputState == Enum.UserInputState.End then
			char:WaitForChild("Humanoid").WalkSpeed = 16
			track.Looped = false
			track:Stop()
		end
	end
end
 
--You can also stuff as many keybinds as you want with more commas
ContextActionService:BindAction("Sprint", handleAction, true, Enum.KeyCode.LeftShift, Enum.KeyCode.L)
--keybinds as a table
local someKeybinds = {Enum1, Enum2, Enum3}
ContextActionService:BindAction("Sprint", handleAction, true, table.unpack(someKeybinds))

For unbinding with CAS:

--Unbind it
ContextActionService:UnbindAction("Sprint")

Comparison for unbinding with inputservices

--vs with input service
inputBegan:Disconnect()
inputEnded:Disconnect()
1 Like

image

local mouse=game.Players.LocalPlayer:GetMouse()
local fire = script.Parent:WaitForChild('fire')
local tool = script.Parent
local uis = game:GetService("UserInputService")
local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local track
tool.Activated:Connect(function()
	fire:FireServer(mouse.Target)
end)

local ContextActionService = game:GetService("ContextActionService")

tool.Equipped:Connect(function()
	local function handleAction(actionName, inputState, inputObject)
		if actionName == "Sprint" then
			if inputState == Enum.UserInputState.Begin then
				char:WaitForChild("Humanoid").WalkSpeed = 25
				track = char.Humanoid:LoadAnimation(script.Parent.Return.Running)
				track.Priority = Enum.AnimationPriority.Idle
				track.Looped = true
				track:Play()
			end

			if inputState == Enum.UserInputState.End then
				char:WaitForChild("Humanoid").WalkSpeed = 16
				track.Looped = false
				track:Stop()
			end
		end
	end

	ContextActionService:BindAction("Sprint", handleAction, true, Enum.KeyCode.LeftShift, Enum.KeyCode.L)

end)

tool.Unequipped:Connect(function()
	ContextActionService:UnbindAction("Sprint")
	track.Looped = false
	track:Stop()
end)

Doesnt seem to work

This is what happening now-

The animation only plays when the gun is equipped, but the speed[sprint] is still on when I unequip the gun.

video:

Tho, it’s a nice service

debounces and animations better to use on client
change paths to the wanted

Client
local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
local RS = game:GetService("RunService")

local Player = Players.LocalPlayer
local Character = Player.CharacterAppearanceLoaded:Wait() and Player.Character
local Humanoid= Character:WaitForChild("Humanoid")
local Animator = Humanoid:WaitForChild("Animator")

local Mouse = Player:GetMouse()
local fire = script:WaitForChild('fire')

local Tool = script.Parent

------------------ Animations ---------------------

local Animations = {
	["Run"] = {
		Priority = Enum.AnimationPriority.Movement,
		Animation = script.Run,
		Looped = true
	},
	["Idle"] = {
		Priority = Enum.AnimationPriority.Idle,
		Animation = script.Idle,
		Looped = true
	},
	["Attack"] = {
		Priority = Enum.AnimationPriority.Action,
		Animation = script.Attack,
		Looped = false
	},
}
for Name, Value in pairs(Animations) do
	local Track = Animator:LoadAnimation(Value.Animation)
	Track.Priority = Value.Priority
	Track.Looped = Value.Looped
	Animations[Name] = Track
end

--------------------------------------------------

local Equipped = false

Tool.Activated:Connect(function()
	Animations.Attack:Play()
	fire:FireServer(Mouse.Target)
end)

Tool.Equipped:Connect(function()
	Animations.Idle:Play()
	Equipped = true
end)

Tool.Unequipped:Connect(function()
	Animations.Idle:Stop()
	Equipped = false
end)

--------------------------------------------------

local KeyCode = Enum.KeyCode.LeftShift
local Debounce = false

UIS.InputBegan:Connect(function(key,processed)
	if processed or not Equipped then return end
	if key.KeyCode == Enum.KeyCode.LeftShift then
		if not Debounce then
			Debounce = not Debounce

			Humanoid.WalkSpeed = 25
			Animations.Run:Play()

			repeat RS.Heartbeat:Wait()
			until not UIS:IsKeyDown(KeyCode)

			Humanoid.WalkSpeed = 16
			Animations.Run:Stop()

			task.wait(0.5)
			Debounce = not Debounce
		end
	end
end)
Server
local Debris = game:GetService("Debris")

local Conf = script.Parent:WaitForChild("Configuration")
local fire = script.Parent:WaitForChild("fire")
local Tool = script.Parent.Parent

fire.OnServerEvent:Connect(function(Player)
	local num = math.random(1, 2)
	if num == 1 then
		Conf.Hit:Play()
	else
		Conf.Hit2:Play()
	end
end)

Nice, tho the remote event is essential there.
[for the raycasting and hitting part]

Whoops, thanks for catching on to that.

Just set the speed back to normal on unequip.

tool.Unequipped:Connect(function()
	ContextActionService:UnbindAction("Sprint")
	char:WaitForChild("Humanoid").WalkSpeed = 16
	track.Looped = false
	track:Stop()
end)
1 Like

Oh, works really neat. So basically this service is useful when we want to execute multiple/different animations/actions at once?

Tho, what is this part, say we had 3 keys, A,B,C in that table, what would that do?

It would function the same for those keys as well.

Table.unpack just seperates the values via comma needed for the CAS function to work (lua “tuples”)

-Assign Multiple keycodes to one specific action
-Mobile support button (not that good)
-Bind and unbind easily
-Debug F9 list the keycodes easily

1 Like

Oh alright, thank you!

Thank you too @V_ladzec and @BuilderBob25620

Worth noting that if the ‘L’ and/or ‘Left Shift’ key(s) is/are held before the action is bound and released after the binding the bound action will be triggered but the ‘track’ upvalue will still point to nil.

This can be remedied with an ‘if track then’ beforehand.

2 Likes