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

Why Won't This Script for a Remote-Controlled Tank Work?

Asked by
8391ice 91
7 years ago

As the title implies, my game consists of RC tanks. This script in particular is supposed to control the tank; make it move forward, left, right, etc. Right now, I am trying to make it so that when it presses W or S, it changes what is known as the "VelocityConstant," a value I'll later use to make the tank move forwards and backwards. However, when I test the script, nothing happens. I've tried making it print in some areas to isolate the problem and I feel that I have possibly typed the ContextActionService connect functions at the very bottom incorrectly. However, I really am not sure.

player = game.Players.LocalPlayer
control = player:WaitForChild("Controlling", 120) --If this value is true, the player can control the tank.
print("control") --works

local velocityConstant = 0
local gasDown = false
local brakeDown = false
local plane =  nil

local maxForwardSpeed = 40
local maxReverseSpeed = -30

local keyUp = nil
local keyDown = nil

function controlChanged()
    if control.Value == true then
        local findTank = game.Workspace.Tanks:FindFirstChild(player.Name)
        if findTank then
            findTank.Velocity.Value = velocityConstant --This will set the Tank's actual NumberValue to the VelocityConstant
        else --If there is no tank with the player's name, control is turned off, because there's nothing to control.
            control.Value = false
        end
    end
end

function keyW(actionName, userInputState, inputObject) --Making a function for each key... is there a better way?
    print("w") --does not print?
    if control.Value == true then
        if userInputState == Enum.UserInputState.Begin then --When W is pressed
            gasDown = true
            while gasDown do
                velocityConstant = maxForwardSpeed
                wait(.03)
            end
        elseif userInputState == Enum.UserInputState.End then --When W is let go
            gasDown = false
            velocityConstant = 0
        end
    end
    controlChanged()
end

function keyS(actionName, userInputState, inputObject)
    if control.Value == true then
        if userInputState == Enum.UserInputState.Begin then --When S is pressed
            brakeDown = true
            while brakeDown do
                velocityConstant = maxForwardSpeed
                wait(.03)
            end
        elseif userInputState == Enum.UserInputState.End then --When S is let go
            brakeDown = false
            velocityConstant = 0
        end
    end
    controlChanged()
end

function keyA(actionName, userInputState, inputObject) --We don't need to worry about A or D right now.
    if control.Value == true then
        local findTank = game.Workspace.Tanks:FindFirstChild(player.Name)
        if findTank then
            if userInputState == Enum.UserInputState.Begin then
                findTank.ControlPart.AngularVelocity = Vector3.new(0, 1.2, 0)
            elseif userInputState == Enum.UserInputState.End then
                findTank.ControlPart.AngularVelocity = Vector3.new(0, 0, 0)
            end
        else
            control.Value = false
        end
    end
    controlChanged()
end

function keyD(actionName, userInputState, inputObject)
    if control.Value == true then
        local findTank = game.Workspace.Tanks:FindFirstChild(player.Name)
        if findTank then
            if userInputState == Enum.UserInputState.Begin then
                findTank.ControlPart.AngularVelocity = Vector3.new(0, -1.2, 0)
            elseif userInputState == Enum.UserInputState.End then
                findTank.ControlPart.AngularVelocity = Vector3.new(0, 0, 0)
            end
        else
            control.Value = false
        end
    end
    controlChanged()
end

game.ContextActionService:BindAction("keyPress", keyW, false, Enum.KeyCode.W) --Is this perhaps wrong?
game.ContextActionService:BindAction("keyPress", keyA, false, Enum.KeyCode.A)
game.ContextActionService:BindAction("keyPress", keyS, false, Enum.KeyCode.S)
game.ContextActionService:BindAction("keyPress", keyD, false, Enum.KeyCode.D)
0
You might want to use UserInputService: http://wiki.roblox.com/index.php?title=API:Class/UserInputService bno23 30 — 7y

Answer this question