#Button to temporarily increase player speed.

1 messages · Page 1 of 1 (latest)

crystal horizon
#

Hello, I've been trying to work with buttons and figure out how they work, I'm a complete newcomer, so I'm trying something easy, which is simply to increase a players speed for a short amount of time by pressing a button. However, every attempt I do breaks something, or simply fails to work. I just haven't been able to figure out how to make the buttons affect the player. If anyone could give me assistance, I'd greatly appreciate it.

Local Script for ScreenGUI

-- ABILITY BUTTONS
    local Ability1 = script.Parent.ScreenGui.Ability1

-- Ability 1
    local Ability1 = script.Parent.ScreenGui.Ability1
        Ability2.MouseButton1Click:Connect(function()

        print('Lets pick up the pace!')
    end)```

**Player Local Script**

-- Shift to Run
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local Speed1 = 30
local Speed2 = 15
humanoid.WalkSpeed = Speed1

-- Run Button
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.LeftShift then
humanoid.WalkSpeed = Speed2
end
end)

UserInputService.InputEnded:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.LeftShift then
        humanoid.WalkSpeed = Speed1
    end
end)

Most of my code here is borrowed from other sources, and I've been trying to tinker with it to learn how it works, but I've gotten stuck on this.
agile verge
#
Ability2.MouseButton1Click:Connect(function()
  humanoid.WalkSpeed = Speed2
  print('Lets pick up the pace!')
end)
crystal horizon
#

Ah, i meant for the ability2 part to be named Ability 1

    local Ability1 = script.Parent.ScreenGui.Ability1
        Ability1.MouseButton1Click:Connect(function()
        humanoid.WalkSpeed = Speed3
        
        print('Lets pick up the pace!')
    end)```

In player, I made a specific player 3 speed
`local Speed3 = 60`

Because Speed1 and Speed2 are Running/Walking toggles via shift, so setting the speed to an already done speed wouldn't do much, haha
#

So, i guess what I'm looking for here is a way to add speed to a player, for a short period, from a button

agile verge
#

use task.delay()

#
task.delay(<AmountOfTime>, function()

end)

Is the syntax

crystal horizon
#

Understood, although, the solution to set a players speed you gave doesn't seem to of worked.

agile verge
#

Can you send me what you wrote?

crystal horizon
#
-- Shift to Run
    local player = game.Players.LocalPlayer
    local character = player.Character or player.CharacterAdded:Wait()
    local humanoid = character:WaitForChild("Humanoid")
    local Speed1 = 30
    local Speed2 = 15
    local Speed3 = 600
    humanoid.WalkSpeed = Speed1```

-- ABILITY BUTTONS
local Ability1 = script.Parent.ScreenGui.Ability1

-- Ability 1
local Ability1 = script.Parent.ScreenGui.Ability1
Ability1.MouseButton1Click:Connect(function()
humanoid.WalkSpeed = Speed3

    print('Lets pick up the pace!')
end)```

I set Speed3 to be absurd, but after clicking the button, it's done nothing.

soft valleyBOT
#

studio** You are now Level 1! **studio

agile verge
crystal horizon
#

Alright, that didn't work either,but I think I found the culprit

I'm getting this error here from the debug log, no clue on what it means

agile verge
crystal horizon
#
-- ABILITY BUTTONS
    local Ability1 = script.Parent.Ability1

-- Ability 1
    local Ability1 = script.Parent.Ability1
    Ability1.MouseButton1Click:Connect(function()
        humanoid.WalkSpeed = Speed3

        print('Lets pick up the pace!')
    end)

Fixed that issue, and the error is no longer popping up! Yet still, it's not working somehow?

#

I apologize for all this hassle, and thank you for still helping

agile verge
crystal horizon
#

Moving the print above the line does make it work, so it's entirely the image above, I assume because the variable exists in the player script, and not the button script?

agile verge
#

Yeah you'll have to reference the humanoid again in there just like Speed3

crystal horizon
#

Yeah, I tried to make that obvious with how i formatted, seperating the two and stating they're different, apologies!

crystal horizon
#

So like this?
game.Players.LocalPlayer.humanoid.WalkSpeed = Speed3

agile verge
crystal horizon
#

Ah, finally got the speed working! Just threw my fella off the baseplate

agile verge
#

If you run into problems implementing the task.delay let me know 👍

crystal horizon
#

Already working on it, again, thank you so much for your patience!

obsidian wren
#

you should switch to a module script

#

from my understanding u press the gui and get a speed boost

crystal horizon
soft valleyBOT
#

studio** You are now Level 2! **studio

obsidian wren
#

yes

#

it allows you to reuse blocks of code across all ur scripts

obsidian wren
#

ofc replacing script name with ur scripts atual name

#

and function name with the name of the function that does stuff

#

-- Shift to Run
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local Speed1 = 30
local Speed2 = 15
humanoid.WalkSpeed = Speed1

-- Run Button
UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
if gameProcessedEvent then return end
if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.LeftShift then
humanoid.WalkSpeed = Speed2
end
end)

UserInputService.InputEnded:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.LeftShift then
        humanoid.WalkSpeed = Speed1
    end
end)

so this bit

#

would be the function

#

(assuming you havent changed it)

crystal horizon
#

I have not just yet, but that seems like a charm, thank you very much for the tip!!

obsidian wren
#

ye

#

u dont gotta switch it tbh

#

but in the future

#

modules are VERYYYYY useful

crystal horizon
#

Making code accessible and modular helps with future stuff

obsidian wren
#

ye also reduces how many remoteevents u use which is good for performance