Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Anyway i can make this activation/deactivation to work?

Asked by
Jirozu 71
7 years ago
Edited 7 years ago

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?

0
First, don't use `Mouse.KeyDown`. Use `UserInputService.InputBegan` instead. The former is deprecated. Link150 1355 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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.
Ad

Answer this question