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

Anyway i can make this activation/deactivation to work?

So i tired to make a move where the character moves faster, here is the code:

Player = game.Players.LocalPlayer
Mouse = Player:GetMouse()
Activate = false

--Activate
Mouse.KeyDown:connect(function(key)
	key = key:lower()
	if key ~= "z" then return end;
	if Activate == false then
		Player.Character.Humanoid.WalkSpeed = Player.Character.Humanoid.WalkSpeed +35
		Activate = true
	end
end)

--Deactivation
Mouse.KeyDown:connect(function(key)
	key = key:lower()
	if key ~= "z" then return end;
	if Activate == true then
		Player.Character.Humanoid.WalkSpeed = Player.Character.Humanoid.WalkSpeed -35
		Activate = false
	end
end)

So is there anyway, to make the activation and deactivation work?

1 comment

0

First, don't use Mouse.KeyDown. Use UserInputService.InputBegan instead. The former is deprecated.

1 answer

✓ Accepted answer

Just use one function

There's no point in using two. Use an else statement to check if the bool is true or false.

Player = game.Players.LocalPlayer
Mouse = Player:GetMouse()
Activate = false

--Activate
Mouse.KeyDown:connect(function(key)
	key = key:lower()
	if key ~= "z" then return end;
	if Activate == false then
		Player.Character.Humanoid.WalkSpeed = Player.Character.Humanoid.WalkSpeed +35
		Activate = true
	else
		Player.Character.Humanoid.WalkSpeed = Player.Character.Humanoid.WalkSpeed -35
		Activate = false
	end
end)

This worked for me, however.

Use UserInputService

KeyDown is deprecated, as you will see on the wiki.

I'm not going to explain how UserInputService works. If you're confused, ask another question. However, first read the wiki on the subject.

With using UserInputService, the script looks more like this.

Player = game.Players.LocalPlayer
Activate = false

game:GetService("UserInputService").InputBegan:connect(function(input)
	if input.KeyCode ~= Enum.KeyCode.Z then return end
	if Activate == false then
		Player.Character.Humanoid.WalkSpeed = Player.Character.Humanoid.WalkSpeed +35
		Activate = true
	else
		Player.Character.Humanoid.WalkSpeed = Player.Character.Humanoid.WalkSpeed -35
		Activate = false
	end
end)
I tried to make is simple, and this actually shortens your script.

Good Luck!

If I helped, please don't forget to accept my answer.

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