Hi, I just created a script that spawns airdrops. I now have no idea how to make this script clearer and better optimized.
Script in ServerScriptService:
local debris: Debris = game:GetService("Debris")
local players: Players = game:GetService("Players")
local serverStorage: ServerStorage = game:GetService("ServerStorage")
local tweenService: TweenService = game:GetService("TweenService")
local airdropWeapons = serverStorage:FindFirstChild("AirdropWeapons"):GetChildren()
local tweenInfo: TweenInfo = TweenInfo.new(15, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local function giveWeapon(player: Player)
local backpack: Backpack = player:FindFirstChildOfClass("Backpack") :: Backpack
local randomWeapon: Tool = airdropWeapons[math.random(1, #airdropWeapons)]:Clone()
if backpack:FindFirstChild(tostring(randomWeapon)) then randomWeapon:Destroy() giveWeapon(player) return end
randomWeapon.Parent = backpack
end
local function spawnAirdrop()
local airdropRandomPosition: Vector3 = Vector3.new(math.random(-100, 100), 100, math.random(-100, 100))
local rayOrigin: Vector3 = airdropRandomPosition
local rayDestination: Vector3 = airdropRandomPosition - Vector3.new(0, 200, 0)
local raycastParams: RaycastParams = RaycastParams.new()
raycastParams.RespectCanCollide = true
local rayDirection: Vector3 = (rayDestination - rayOrigin)
local raycastResult: RaycastResult = workspace:Raycast(rayOrigin, rayDirection)
if not raycastResult then spawnAirdrop() return end
if raycastResult.Position.Y > 0 then spawnAirdrop() return end
if tostring(raycastResult.Instance) == "Water" then spawnAirdrop() return end
local airdrop: Model = script.Airdrop:Clone()
airdrop:PivotTo(CFrame.new(airdropRandomPosition) * CFrame.Angles(0, 0, math.rad(90)))
airdrop.Parent = workspace
local primaryPart: BasePart = airdrop.PrimaryPart :: BasePart
local function createTween()
local tween:Tween = tweenService:Create(primaryPart, tweenInfo, {CFrame = CFrame.new(raycastResult.Position + Vector3.new(0, (primaryPart.Size.Y / 2) + 4.5, 0)) * CFrame.Angles(0, 0, math.rad(90))})
tween:Play()
tween.Completed:Wait()
local parachute: Model = airdrop:FindFirstChild("Parachute") :: Model
parachute:Destroy()
local crate: Model = airdrop:FindFirstChild("Crate") :: Model
local cratePrimaryPart: BasePart = crate.PrimaryPart :: BasePart
local proximityPrompt: ProximityPrompt = Instance.new("ProximityPrompt")
proximityPrompt.ActionText = "Open"
proximityPrompt.HoldDuration = 5
proximityPrompt.ObjectText = "Crate"
proximityPrompt.RequiresLineOfSight = false
proximityPrompt.Parent = cratePrimaryPart
proximityPrompt.Triggered:Connect(function(playerWhoTriggered: Player)
airdrop:Destroy()
giveWeapon(playerWhoTriggered)
end)
debris:AddItem(airdrop, 60)
end
local createTweenCoroutine = coroutine.create(createTween)
coroutine.resume(createTweenCoroutine)
end
while true do
spawnAirdrop()
local playersAmount: number = #players:GetPlayers()
local minWaitTime: number = 30
local maxWaitTime: number = 60
for index: number = 1, playersAmount do
minWaitTime -= 3
maxWaitTime -= 6
end
task.wait(math.random(minWaitTime, maxWaitTime))
end