I’ve been scripting for around a month or two now, and I’m not sure if I’m improving or not. I’ve been reading and watching a lot of tutorials and I just feel like I get stuck a lot, I understand that scripting will take awhile to get better at but I believe direct feedback will allow me to progress and learn better
This code is in a LocalScript under StarterPlayerScripts. Basically what it does is whenever a player touches a spawner within the game, the spawner will fire to the client which course, hole, and direction the ball should be spawned at, the spawner will then turn red signaling that the ball has been spawned and can only despawn the ball if the ball is put in the corresponding hole or the player cancels the hole by clicking the spawner while it’s red. The game I’m trying to make is a very very simple golf game where a player has a tool that only collides with a ball and the player has to try to get the ball into the hole. There’s a bunch of other stuff but that’s not relevant to the script.
--*&@&*--
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-- Variables
--=-=-=-=-=-=-=-- Services
local replicatedStorage = game:GetService("ReplicatedStorage")
local physicsService = game:GetService("PhysicsService")
local tweenService = game:GetService("TweenService")
--=-=-=-=-=-=-=-- Services
--=-=-=-=-=-=-=-- Instances & Events
local spawnEvent = replicatedStorage.RemoteEvents:WaitForChild("SpawnEvent")
local ball = replicatedStorage.Instances:WaitForChild("Ball")
local plr = game.Players.LocalPlayer
local plrName = plr.Name
--=-=-=-=-=-=-=-- Instances & Values
--=-=-=-=-=-=-=-- Config
local tweenTime = 2
local spawnOffset = 2.5
local maxActivationDistance = 50
local cancelCooldown = 1
local tweenInfo = TweenInfo.new(tweenTime)
--=-=-=-=-=-=-=-- Config
--=-=-=-=-=-=-=-- Debounces
local canSpawn = true
local inHole = false
local cancelled = false
--=-=-=-=-=-=-=-- Debounces
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-- Variables
--*&@&*--
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-- RemoteEvent fired
spawnEvent.OnClientEvent:Connect(function(c,h,d)
if canSpawn == true then
canSpawn = false
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-- Player touched spawner
local course = workspace.World.Courses:FindFirstChild("Course"..c)
if course then
local hole = course.Holes:FindFirstChild("Hole"..h)
if hole then
print("Succesfully loaded Hole"..h)
--=-=-=-=-=-=-=-- Hole variables
local spawner = hole:FindFirstChild("Spawner")
local holeDisplay = hole:WaitForChild("HoleDisplay")
local display = holeDisplay:WaitForChild("Status")
local status = display:WaitForChild("Completed")
local spawnerColor = spawner.Color
local clickDetector = spawner:WaitForChild("ClickDetector")
local highlight = spawner:WaitForChild("Highlight")
--=-=-=-=-=-=-=-- Hole variables
spawner.Color = Color3.fromRGB(255)
highlight.Enabled = true
clickDetector.MaxActivationDistance = maxActivationDistance
spawner.Spawn:Play()
local clone = ball:Clone()
clone.Parent = workspace.ActiveBalls
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-- Player touched spawner
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-- Checks what direction the ball should spawned in
if d == "back" then
clone.Position = spawner.Position + Vector3.new(0,0,spawnOffset)
elseif d == "front" then
clone.Position = spawner.Position + Vector3.new(0,0,-spawnOffset)
elseif d == "right" then
clone.Position = spawner.Position + Vector3.new(spawnOffset,0,0)
elseif d == "left" then
clone.Position = spawner.Position + Vector3.new(-spawnOffset,0,0)
end
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-- Checks what direction the ball should spawned in
--=-=-=-=-=-=-=-- Checks if a player cancels
clickDetector.MouseClick:Connect(function()
if cancelled == false then
cancelled = true
clone:Destroy()
print("Cancelled Hole"..h)
spawner.Cancel:Play()
spawner.Color = spawnerColor
highlight.Enabled = false
clickDetector.MaxActivationDistance = 0
canSpawn = true
task.wait(cancelCooldown)
cancelled = false
end
end)
--=-=-=-=-=-=-=-- Checks if a player cancels
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-- Ball made it into hole
clone.Touched:Connect(function(hit)
if hit.Name == hole.Name then
if inHole == false then
if status.Value == false then
status.Value = true
display.Color = Color3.fromRGB(0,255,0)
end
print("Ball made it into Hole"..h)
inHole = true
local tween = tweenService:Create(clone,tweenInfo,{Transparency = 1})
local particle = hit:FindFirstChild("ParticleEmitter")
local sound = hit:FindFirstChild("Sound")
clickDetector.MaxActivationDistance = 0
clone.Color = Color3.fromRGB(0,255)
clone.Anchored = true
clone.CanCollide = false
sound:Play()
tween:Play()
particle.Enabled = true
task.wait(0.5)
particle.Enabled = false
task.wait(tweenTime-0.5)
clone:Destroy()
inHole = false
spawner.Color = spawnerColor
highlight.Enabled = false
canSpawn = true
end
end
end)
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-- Ball made it into hole
--=-=-=-=-=-=-=-- Debugging
else
warn("Could not find Hole"..h.."!")
error("There was an error loading specified Hole")
end
else
warn("Could not find Course"..c.."!")
error("There was an error loading specified Hole")
end
--=-=-=-=-=-=-=-- Debugging
end -- If debounce end
end) -- Player added end
--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-- RemoteEvent fired
--*&@&*--
Any critiques or tips?
I would really appreciate if I could get feedback in anyway possible. Right now the script works fine and as intended, however I feel like it’s super messy and can be optimized much better. Is there any flaws, better ways of doing things, and quicker/easier methods? This script basically sums up most of my scripting knowledge that I’ve learned so far by the way.
To sum it up, I need to know if I can make the script better