Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How would I go about make a simple sword block system?

So for my game i was wondering how i could make it so that the player can block when their sword is in hand. I tried doing this but it didnt work. (this is was in the starterplayer scripts) Note:This is just a animation.

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == enum.KeyCode.Q then
	KeyQisBeingPressed = true
end
while KeyQisBeingPressed == true do
	local Animation = player.Humanoid:LoadAnimation(block)
	Animation:Play()
end
end)

UIS.InputEnded:Connect(function(input)
	if input.KeyCode == enum.KeyCode.Q then
	KeyQisBeingPressed = false
end
while KeyQisBeingPressed == false do
	local Animation = player.Humanoid:LoadAnimation(block)
	Animation:Stop()
end)

Variables:KeyQisBeingPressed -- boolean Variables:local block = game.starterpack.classicsword:WaitForChild('BlockAnim')

2 comments

0

Capitalize Enum

0

thats not the problem tho

1 answer

✓ Accepted answer

Your code has one error, but I redid it with a coroutine loop so that you can continue writing below the loop. Use UserInputService's IsKeyDown function when checking key-presses by the way.

local UserInputService = game:GetService("UserInputService")

coroutine.resume(coroutine.create(function()
	while true do
		wait()
		if UserInputService:IsKeyDown(Enum.KeyCode.Q) then
			local Animation = Animation ~= nil and Animation or player.Humanoid:LoadAnimation(block)
			if not Animation.IsPlaying then
				Animation:Play()
			end
		else
			pcall(function() Animation:Stop() end)
		end
	end
end))

This discussion is preserved as it was. New answers, comments, and votes are closed.