Hi, I have made a Pong game in roblox, but I am not entirely satisified with how this code turned out.
Is there any way I could optimize this? Or a better method to do stuff like this?
local ball = workspace:WaitForChild("Ball")
local paddle1 = workspace:WaitForChild("P1_paddle")
local paddle2 = workspace:WaitForChild("P2_paddle")
local gameFunctions = require(script.Parent.GameFunctions)
local replicatedStorage = game:GetService("ReplicatedStorage")
local P1 = replicatedStorage.P1
local P2 = replicatedStorage.P2
local speed = 10
local radius = gameFunctions.ballRadius
local width = gameFunctions.width
local height = gameFunctions.height
local game_width = gameFunctions.width - radius*2
local game_height = gameFunctions.height - radius*2
local paddle_width = paddle1.Size.X
local paddle_height = paddle1.Size.Z
local ball_sound = workspace.BallSound
repeat task.wait() until gameFunctions.isReady() == true
gameFunctions.Start()
game:GetService("RunService").Heartbeat:Connect(function(dt)
ball.CFrame *= CFrame.new(gameFunctions.dX*speed*dt, 0, gameFunctions.dZ*speed*dt)
ball.Position = Vector3.new(math.clamp(ball.Position.X, -game_width, game_width), 0, ball.Position.Z)
--moving the ball and adjusting the position so it doesent go out of bounds
if (math.abs(ball.Position.X) >= game_width/2) then
ball_sound.PlaybackSpeed = math.random(87,116)/100
ball_sound:Play()
gameFunctions.dX *= -1
--wall bounce
end
if (math.abs(ball.Position.Z) >= game_height/2) then
speed = 10
if ball.Position.Z <= 0 then
gameFunctions.AddPoint(P1.Value)
else
gameFunctions.AddPoint(P2.Value)
end
gameFunctions.Stop()
gameFunctions.Start()
-- point scoring
end
if workspace:GetPartsInPart(ball)[1] == paddle1 or workspace:GetPartsInPart(ball)[1] == paddle2 then
ball_sound.PlaybackSpeed = math.random(87,116)/100
ball_sound:Play()
gameFunctions.dZ *= -math.random(9,13) / 10
speed += 0.5
--paddle collision
end
end)