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

Best way to utilize UserInputService?

Asked by 9 years ago

So I've been trying to figure out a way to use the service within the StarterPlayerScripts file. Everything should work all fine and dandy until the player the resets/dies:

Var = 0
Var1 = 0
player = game.Players.LocalPlayer

player.CharacterAdded:connect(function(char)
    char.Humanoid.Died:connect(function()
        repeat wait() until char.Humanoid.Health ~= 0
        Body = Instance.new("BodyVelocity", char.Torso)
        Body.Velocity = Vector3.new(-24,0,0)
        Body.MaxForce = Vector3.new(4000,0,4000)
    end)
end)

game:GetService("UserInputService").InputBegan:connect(function(Key, Condition)
    if player.Character.Humanoid.Health ~= 0 then
        if Key.KeyCode == Enum.KeyCode.D then
            Var = 1
            if Var == 1 and Var1 == 1 then
                Body.Velocity = Vector3.new(-24,0,0)
            elseif Var == 1 then
                Body.Velocity = Vector3.new(-24,0,-24)
            end
        end
        if Key.KeyCode == Enum.KeyCode.A then
            Var1 = 1
            if Var == 1 and Var1 == 1 then
                Body.Velocity = Vector3.new(-24,0,0)
            elseif Var1 == 1 then
                Body.Velocity = Vector3.new(-24,0,24)
            end
        end
    end
end)


game:GetService("UserInputService").InputEnded:connect(function(Key, Condition)
    if player.Character.Humanoid.Health ~= 0 then
        if Key.KeyCode == Enum.KeyCode.D then
            if Var == 1 and Var1 == 1 then
                Body.Velocity = Vector3.new(-24,0,24)
                Var = 0
            elseif Var == 1 and Var1 == 0 then
                Body.Velocity = Vector3.new(-24,0,0)
                Var = 0
            end
        end
        if Key.KeyCode == Enum.KeyCode.A then
            if Var == 1 and Var1 == 1 then
                Body.Velocity = Vector3.new(-24,0,-24)
                Var1 = 0
            elseif Var1 == 1 and Var == 0 then
                Body.Velocity = Vector3.new(-24,0,0)
                Var1 = 0
            end
        end
    end
end)


Now I know why the script doesn't work, but what I'm asking is, what is the best way to use this service so that it is efficient, simple, and functions even when the player dies. Please HALP!!!

1
What are you trying to do on line 7? adark 5487 — 9y
0
That function checks for if the player has reset (character) and will add body velicoty to it. LateralLace 297 — 9y
0
Woops sorry that's an error ;? . LateralLace 297 — 9y

Answer this question