Hey there! Long story short I made a tiny little game that uses remote events to move a block (that is binded to your character and moved via WASD) across the server.
For now the only “feature” I have implemented is that when your block hits another block, a 5 second timer goes off before you and your block explode along with your opponent.
If you could give any tips on minimizing or reducing any code that I have feel free. I’m a bit rusty to ROBLOX lua
Control your Character via RBXL.rbxl (26.3 KB)
Main script for Remote Control (tool)
--// Variables \\--
local CAS, RS, LocalPlayer = game:GetService("ContextActionService"), game:GetService("ReplicatedStorage"), game:GetService("Players").LocalPlayer
local Bot = game.Workspace:WaitForChild(LocalPlayer.Name .. "-bot")
local moveEvent = RS.MovePart
local tool = script.Parent
local MOVEMENT_KEYS = {
["Forward"] = Enum.KeyCode.W,
["Backwards"] = Enum.KeyCode.S,
["Left"] = Enum.KeyCode.A,
["Right"] = Enum.KeyCode.D
};
local function MoveObject(actionName, inputState, input)
for i, v in pairs(MOVEMENT_KEYS) do
if actionName == i and input.KeyCode == v then
moveEvent:FireServer(actionName, input.KeyCode)
end
end
end
tool.Equipped:Connect(function()
CAS:BindAction("Forward", MoveObject, false, Enum.KeyCode.W)
CAS:BindAction("Backwards", MoveObject, false, Enum.KeyCode.S)
CAS:BindAction("Left", MoveObject, false, Enum.KeyCode.A)
CAS:BindAction("Right", MoveObject, false, Enum.KeyCode.D)
end)
tool.Unequipped:Connect(function()
CAS:UnbindAction("Forward")
CAS:UnbindAction("Backwards")
CAS:UnbindAction("Left")
CAS:UnbindAction("Right")
end)
Server-sided Movement handler
--// Variables \\--
local CAS, RS, Players, TweenService = game:GetService("ContextActionService"), game:GetService("ReplicatedStorage"), game:GetService("Players"), game:GetService("TweenService")
local moveEvent = RS.MovePart
local debounce = false
local MOVEMENT_POSITIONS = {
["Forward"] = Vector3.new(0, 0, 2),
["Backwards"] = Vector3.new(0, 0, -2),
["Left"] = Vector3.new(2, 0, 0),
["Right"] = Vector3.new(-2, 0, 0)
};
--// Events \\--
moveEvent.OnServerEvent:Connect(function(player, direction)
local playerBot = game.Workspace[player.Name .. "-bot"]
if not playerBot then
return
end
if not debounce then
---debounce = true
end
local character = player.Character or player.CharacterAdded:Wait()
if not character:FindFirstChild("Magic Car") then
player:Kick("Illegally firing remotes.")
else
for i, v in pairs(MOVEMENT_POSITIONS) do
if i == direction then
local goal = {}
goal.Position = playerBot.Position + v
local tweenInfo = TweenInfo.new(0.06, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
local tween = TweenService:Create(playerBot, tweenInfo, goal)
tween:Play()
return
end
end
end
end)
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
local playerBot = RS.Bot:Clone()
playerBot.Name = player.Name .. "-bot"
local spawnprotected = Instance.new("BoolValue", playerBot)
spawnprotected.Name = "SpawnProtection"
spawnprotected.Value = true
playerBot.Color = Color3.new(math.random(0, 255), math.random(0, 255), math.random(0, 255))
playerBot.Parent = game.Workspace
playerBot.Position = Vector3.new(math.random(10, 30), math.random(10, 30), math.random(10, 30))
playerBot.Anchored = false
playerBot:SetNetworkOwner(player)
wait(10)
spawnprotected.Value = false
end)
player.CharacterRemoving:Connect(function ()
local bot = game.Workspace:FindFirstChild(player.Name .. "-bot")
if bot then
bot:Destroy()
end
end)
end)
Players.PlayerRemoving:Connect(function(player)
local bot = game.Workspace:FindFirstChild(player.Name .. "-bot")
if bot then
bot:Destroy()
end
end)
Explosion handler
countdownStarted = false
--/. Functions \\--
local function blowupSequence(object, objectOwner)
if object then
local counter = 1
for i = 1, 5, 1 do
print(counter)
if counter > 4 then
object.Color = Color3.new(0.666667, 0, 0)
wait(1)
object.Color = Color3.new(0, 0, 0)
Instance.new("Explosion", object)
Instance.new("Explosion", objectOwner)
if object and objectOwner then
object:Destroy()
objectOwner:FindFirstChild("Humanoid"):TakeDamage(100)
end
else
object.Color = Color3.new(0.666667, 0, 0)
wait(1)
object.Color = Color3.new(0.0117647, 0.0117647, 0.0117647)
wait(1)
counter = counter + 1
end
end
end
end
local connection = script.Parent.Touched:Connect(function(hit)
if not hit.Parent:FindFirstChild("Humanoid") and hit.Name ~= "Baseplate" then
if string.match(hit.Name, "-") then
repeat game:GetService("RunService").Heartbeat:Wait() until hit.SpawnProtection.Value == false
local playerName = hit.Name:sub(1, hit.Name:find("-bot") - 1)
local player = game.Workspace[playerName]
local ownerName = script.Parent.Name:sub(1, hit.Name:find("-bot") - 1)
local owner = game.Workspace[ownerName]
local Thread = coroutine.wrap(blowupSequence)
Thread(hit, player)
blowupSequence(script.Parent, owner)
countdownStarted = false
end
end
end)
I included the RBXL so that people who prefer hands on can directly look at my code. I ask that you please don’t steal anything related to the script as I worked on it for a few hours.
Any and all suggestions are appreciated!