Hey everyone! Welcome to my first post in #help-and-feedback:code-review! Recently, I decided to take a quick break because I and my commissioner realized I don’t always write completely optimized code. I am also trying to build up my skill in the super power section of scripting. Today, I have made a power that lets you lift rocks by pressing “R”.
Here’s a video of it in action:
Now, to the code review part. I wondered if the DevForum community would have ways to optimize this code. Click the drop-downs below to view my code.
Client code. Located in StarterPlayerScripts
--//Assigning Variables\\--
local UIS = game:GetService("UserInputService")
local Rocks = game.ReplicatedStorage.PowerParts.Rocks
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hrp = character:WaitForChild("HumanoidRootPart")
local source = character:WaitForChild("LeftHand")
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.R then
print("Rocks")
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://6077522199"
local track = character.Humanoid:LoadAnimation(animation)
track:Play()
wait(2)
local clone = Rocks.Effect:Clone()
clone.Parent = source
game.ReplicatedStorage.Remotes.RocksServer:FireServer(player.Name)
wait(2)
clone:Destroy()
end
end)
Server code. Located in ServerScriptService
game.ReplicatedStorage.Remotes.RocksServer.OnServerEvent:Connect(function(Event)
local pName = tostring(Event)
local player = game.Players:FindFirstChild(pName)
local character = player.Character
local hrp = character:WaitForChild("HumanoidRootPart")
local rocks = game.ReplicatedStorage.PowerParts.Rocks
local rock1 = rocks.RockOne:Clone()
local rock2 = rocks.RockTwo:Clone()
local rock3 = rocks.RockThree:Clone()
local TweenService = game:GetService("TweenService")
--//Assigning Positions & Playing Tweens\\--
rock1.Parent = workspace
rock2.Parent = workspace
rock3.Parent = workspace
rock1.Position = hrp.Position + Vector3.new(10, -10, 0)
rock2.Position = hrp.Position + Vector3.new(3, -8, 5)
rock3.Position = hrp.Position + Vector3.new(7, -5, 10)
local anim1 = require(rock1.Animator)
local anim2 = require(rock2.Animator)
local amim3 = require(rock3.Animator)
anim1.Move:Play()
anim2.Move:Play()
amim3.Move:Play()
wait(2)
rock1.CanCollide = false
rock2.CanCollide = false
rock3.CanCollide = false
rock1.Anchored = false
rock2.Anchored = false
rock3.Anchored = false
wait(3)
rock1:Destroy()
rock2:Destroy()
rock3:Destroy()
end)
I already can see that some parts of the code are not completely optimized. Things like this:
rock1.CanCollide = false
rock2.CanCollide = false
rock3.CanCollide = false
I would by default like to just use a for loop
, but since it’s in workspace, I can’t use if v:IsA
because it may use all the parts. Does anyone have ways to optimize this? I would love feedback.
Thanks for reading!