We use cookies on this site to enhance your user experience

Sep 04 2018, 1:39 PM PST

--This script determines what functions to call when a player presses a button when playing.

-- Roblox Services
local ContextActionService = game:GetService("ContextActionService")
local Players = game:GetService("Players")

-- Variables for the player, camera, and player’s mouse
local player = Players.LocalPlayer
local camera = workspace.CurrentCamera	
local mouse = player:GetMouse()

-- Configuration variables
-- Sets the player's speed: -200000 will go fast, -20000 will go slow
local PLAYER_SPEED = -45000
local FORWARD_KEY = Enum.KeyCode.W

-- Creates a force vector that uses player's speed
local forwardForceVector = Vector3.new(0,0,PLAYER_SPEED)

-- Movement function that is called when the player presses the move forward button
local function onMove(actionName, inputState)
	if inputState == Enum.UserInputState.Begin then
		-- If the player presses down the button, move forward
		player.Character.HumanoidRootPart.VectorForce.Force = forwardForceVector
	elseif inputState == Enum.UserInputState.End then
		-- If the player releases the button, stop moving 
		player.Character.HumanoidRootPart.VectorForce.Force = Vector3.new(0,0,0)

-- Function that is called when the player moves their mouse or finger
local function onAim()
	-- Make sure the character exists
	if player.Character then
		-- Finds the position of the mouse in the world and rotates the character model to face it
		local rootPart = player.Character:FindFirstChild("HumanoidRootPart")
		local mouseLocation = Vector3.new(mouse.Hit.X, rootPart.Position.Y, mouse.Hit.Z)
		rootPart.CFrame = CFrame.new(rootPart.Position, mouseLocation)

-- Set up control bindings
ContextActionService:BindAction("Aim", onAim, false, Enum.UserInputType.MouseMovement)
ContextActionService:BindAction("Move", onMove, false, FORWARD_KEY)