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

Script Works In Studio But Not Actual Game?

Asked by 10 years ago

I made a script that makes you sprint, it doesn't work well but it is good enough. The only problem is that it only works in the studio. It is in the starter Gui

Script is there

p = game.Players.LocalPlayer
char = Player.Character
Torso = Char.Torso
Mouse = Player:GetMouse()

function toSprint(key)
 key = key:lower()
   if key == "v" then
   char.Humanoid.WalkSpeed = 50
   else 
   char.Humanoid.WalkSpeed = 16
   end
end

Mouse.KeyDown:connect(toSprint)

I have no idea why it doesn't work? I am just learning how to do this so if someone could tell me why it doesn't work that would be great.

1 answer

Log in to vote
0
Answered by
Redbullusa 1580 Moderation Voter
10 years ago

Normally I'd just insert a script inside the character itself, because I'm used to it. But here's a set of code you could try. I've tested it and it works in the server. As long as this is in a LocalScript, it should execute fine.

p = game.Players.LocalPlayer    -- I've set 'p' as the player.

-- There are other ways to wait for the player. I used "repeat ... until [condition]" because I'm used to it.
repeat
    wait()      -- This keeps waiting until the instant that 'p' is fully rendered.
until p

Mouse = p:GetMouse()

ToggleSprint = false    -- Note this static variable.

function toSprint(key)
    key = key:lower()
    local char = p.Character
    if key == "v" then
        if ToggleSprint == false then       -- This is saying "If ToggleSprint is false, then make it true and have the WalkSpeed go 50. Otherwise, revert back to default speed."
            ToggleSprint = true
            char.Humanoid.WalkSpeed = 50
        else                                -- Kind of like a light-switch effect. You can turn it on or off by the click of the 'v' button.
            ToggleSprint = false
            char.Humanoid.WalkSpeed = 16
        end
    end
end

Mouse.KeyDown:connect(toSprint)
Ad

Answer this question