Hello, I just finished the biggest script I’ve made, 213 lines of code. I’m wondering what are your thoughts on it and how can I improve my code. 213 lines is big for me but is it above 200 because I made some things overcomplicated? Here’s the script:
- Main script in the fishing rod
local tool = script.Parent
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local RepSto = game:GetService("ReplicatedStorage")
local TwSer = game:GetService("TweenService")
local fishGui = Players.LocalPlayer.PlayerGui.epicFISHING
local noFish = Players.LocalPlayer.PlayerGui.NoFish
local DaGui = Players.LocalPlayer.PlayerGui.BULLSHARK
local IsUsingRod = false
local IDK = false
local MAX_MOUSE_DISTANCE = 110
local MAX_ROD_DISTANCE = 100
local force
local TwInfo = TweenInfo.new(1.5)
local Tween = TwSer:Create(DaGui.Frame.ImageLabel, TwInfo, {Position = UDim2.new(.945,0,0,0)})
local fish
local hitPosition
local function getWorldMousePosition()
local mouseLocation = UserInputService:GetMouseLocation()
local screenToWorldRay = workspace.CurrentCamera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
local directionVector = screenToWorldRay.Direction * MAX_MOUSE_DISTANCE
local weaponRaycastParams = RaycastParams.new()
weaponRaycastParams.FilterDescendantsInstances = {Players.LocalPlayer.Character}
local raycastResult = workspace:Raycast(screenToWorldRay.Origin, directionVector, weaponRaycastParams)
if raycastResult then
return raycastResult.Position
end
end
local function GetRandomFish()
if force <= 25 then
return RepSto:FindFirstChild("Basic fish")
elseif force <= 30 then
return RepSto:FindFirstChild("Rare fish")
elseif force <= 34 then
return RepSto:FindFirstChild("Shiny fish")
elseif force <= 38 then
return RepSto:FindFirstChild("Baby mako")
elseif force <= 40 then
return RepSto.Bananalligator
else
return
end
end
local function UseRod()
local mouseLocation = getWorldMousePosition()
if mouseLocation then
local targetDirection = (mouseLocation - tool.Handle.Position).Unit
local directionVector = targetDirection * MAX_ROD_DISTANCE
local weaponRaycastParams = RaycastParams.new()
weaponRaycastParams.FilterDescendantsInstances = {Players.LocalPlayer.Character}
local weaponRaycastResult = workspace:Raycast(tool.Handle.Position, directionVector, weaponRaycastParams)
if weaponRaycastResult then
hitPosition = weaponRaycastResult.Position
local Material = weaponRaycastResult.Material
if Material == Enum.Material.Water and hitPosition and Players.LocalPlayer:GetAttribute("Baits") >= 1 and IsUsingRod == false then
IsUsingRod = true
force = math.random(10, 41)
Players.LocalPlayer:SetAttribute("Baits", Players.LocalPlayer:GetAttribute("Baits") - 1)
local Animator = Players.LocalPlayer.Character.Humanoid.Animator
local Swing = Instance.new("Animation")
Swing.AnimationId = "rbxassetid://12646581867"
local SwingTrack = Animator:LoadAnimation(Swing)
SwingTrack.Priority = Enum.AnimationPriority.Action
SwingTrack.Looped = false
SwingTrack:Play()
SwingTrack:AdjustSpeed(.35)
Players.LocalPlayer.Character.Humanoid.WalkSpeed = 0
game.ReplicatedStorage.FISHING:FireServer(hitPosition)
wait(5)
if force >= 20 then
fishGui.Force.Text = ("Fish's force: "..force)
fishGui.Enabled = true
else
noFish.Enabled = true
end
game.ReplicatedStorage.FISHING.OnClientEvent:Wait()
noFish.Enabled = false
fishGui.Enabled = false
Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
IsUsingRod = false
end
end
end
end
tool.Activated:Connect(UseRod)
local function Fight()
local PosOfDaGui = DaGui:WaitForChild("Frame").ImageLabel.Position
if PosOfDaGui.X.Scale >= .41 and PosOfDaGui.X.Scale <= .51 then
RepSto.BullSharkFished:FireServer(DaGui)
else
RepSto.BullSharkFished:FireServer(Players.LocalPlayer.Character)
end
return
end
fishGui.Force.Activated:Connect(function()
if IDK == false then
IDK = true
fish = GetRandomFish()
end
force = force - 1
fishGui.Force.Text = ("Fish's force: "..force)
if force == 0 then
fishGui.Enabled = false
if fish then
RepSto.BoughtSomething:FireServer(fish)
else
local Anim = Instance.new("Animation")
Anim.AnimationId = "rbxassetid://12892091586"
local AnimTrack = Players.LocalPlayer.Character.Humanoid.Animator:LoadAnimation(Anim)
AnimTrack.Priority = Enum.AnimationPriority.Action2
AnimTrack.Looped = false
if tool then
script.Parent = Players.LocalPlayer.PlayerScripts
tool:Destroy()
end
game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
AnimTrack:Play()
AnimTrack:AdjustSpeed(.45)
AnimTrack.Stopped:Wait()
game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
Players.LocalPlayer.PlayerGui.BlackScreen.Enabled = true
game.SoundService.WaterSplash:Play()
local place = Vector3.new(54.342, -21.323, -96.222)
RepSto.BullSharkFished:FireServer(nil,place)
wait(3)
Players.LocalPlayer.PlayerGui.BlackScreen.Enabled = false
local HasChosenDialog = false
while HasChosenDialog == false do
game.Workspace.Gorilla.Head.Help.DialogChoiceSelected:Connect(function(player, dialog)
if player == Players.LocalPlayer and dialog.Name == "ItComes" then
HasChosenDialog = true
end
end)
wait(.1)
end
wait(3)
while true do
local Shark = RepSto.BullShark:Clone()
Shark.Parent = game.Workspace
local STween = TwSer:Create(Shark, TweenInfo.new(1.5),{Position = Players.LocalPlayer.Character.HumanoidRootPart.Position})
STween:Play()
DaGui.Enabled = true
Tween:Play()
Tween.Completed:Wait()
if DaGui.Enabled == true then
DaGui.Enabled = false
Fight()
end
Shark:Destroy()
DaGui.Frame.ImageLabel.Position = UDim2.new(0,0,0,0)
HasChosenDialog = true
RepSto.BullSharkFished.OnClientEvent:Connect(function()
script:Destroy()
end)
wait(math.random(3,6))
end
end
IDK = false
end
end)
local function CheckInput(Input)
local Animate = false
if DaGui.Enabled == true then
if Input:IsA("InputObject") then
if Input.KeyCode == Enum.KeyCode.E then
Animate = true
elseif Input.KeyCode == Enum.KeyCode.ButtonX then
Animate = true
end
else
Animate = true
end
end
if Animate == true then
DaGui.Enabled = false
local Animator = Players.LocalPlayer.Character.Humanoid.Animator
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://12779583215"
local animationtrack = Animator:LoadAnimation(animation)
animationtrack:Play()
animationtrack.Stopped:Wait()
Fight()
end
Animate = false
end
UserInputService.TouchTap:Connect(CheckInput)
UserInputService.InputBegan:Connect(CheckInput)
game:GetService("RunService").Heartbeat:Connect(function()
if workspace.CurrentCamera.CameraType == Enum.CameraType.Scriptable then
workspace.CurrentCamera.CFrame = Players.LocalPlayer.Character.Head.CFrame * CFrame.new(0,0,-.5)
end
end)
Players.LocalPlayer.Character.Humanoid.Died:Connect(function()
local Shark = game.Workspace:FindFirstChild("BullShark")
if Shark then
Shark:Destroy()
end
script:Destroy()
end)
*The other script, was originally 2 scripts but I fused them together
local Event = game.ReplicatedStorage.BullSharkFished
local Punchs = 0
Event.OnServerEvent:Connect(function(player, Variable, place)
if not Variable then
player.Character.HumanoidRootPart.Position = place
else
if Variable:IsA("Model") then
if Variable:FindFirstChild("LeftFoot") then
Variable.LeftFoot:Destroy()
Variable.LeftUpperLeg:Destroy()
Variable.LeftLowerLeg:Destroy()
elseif Variable:FindFirstChild("RightFoot") then
Variable.RightFoot:Destroy()
Variable.RightUpperLeg:Destroy()
Variable.RightLowerLeg:Destroy()
elseif Variable:FindFirstChild("LeftHand") then
Variable.LeftHand:Destroy()
Variable.LeftUpperArm:Destroy()
Variable.LeftLowerArm:Destroy()
elseif Variable:FindFirstChild("RightHand") then
Variable.RightHand:Destroy()
Variable.RightUpperArm:Destroy()
Variable.RightLowerArm:Destroy()
else
Variable.Head:Destroy()
end
else
Punchs += 1
if Punchs == 3 then
Punchs = 0
Event:FireClient(player)
local Shark = game.ReplicatedStorage.BullSharkTool:Clone()
Shark.Name = "Bull shark"
Shark.Parent = player.Backpack
end
end
end
end)
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local TweenSer = game:GetService("TweenService")
local Event2 = game.ReplicatedStorage.FISHING
Event2.OnServerEvent:Connect(function(player, GoalPos)
local Rod = player.Backpack:FindFirstChild("Fishing rod")
if not Rod then
Rod = player.Character:FindFirstChild("Fishing rod")
end
wait(1)
Rod.FishHook.Ball.Weld.Enabled = false
local TwIn = TweenInfo.new(1)
local goal = {CFrame = CFrame.new(GoalPos)}
local Tween = TweenSer:Create(Rod.FishHook.Ball, TwIn, goal)
Tween:Play()
Tween.Completed:Wait()
wait(8)
Rod.FishHook:PivotTo(Rod.Handle.CFrame * CFrame.new(0,-1,0))
Rod.FishHook.Ball.Weld.Enabled = true
Event2:FireClient(player)
end)
Thanks for reading.