Loadout script breaks weapons

Hello, i’m ShyowwTheDev (AKA Shyoww). I’ve been making a randomizer game for the past 4/5 days, which provides a melee, a ranged and a miscellaneous. And, i encountered a bug, which makes the player’s weapons not work.
More information down below:

  • What does the code do and what are you not satisfied with?
    The code gives you a loadout of weapons, like item asylum, randomizer etc. For the fact of the weapons not working when they’re in a specific folder, and, when i put the weapons on StarterPack, they work normally.

  • What potential improvements have you considered?
    Changing the folders location, updating the code but nothing seems to work.

  • How (specifically) do you want to improve the code?
    Making the weapons to work, since it’s a randomizer game, and if the weapons don’t work, the game won’t receive any visits or positive reviews.

Here’s a look at the code if anyone needs it.

-- // services and variables
local players = game:GetService("Players")
local player = players.LocalPlayer
local replicatedstorage = game:GetService("ReplicatedStorage")

-- // functions
local loadout1 = replicatedstorage.storage.melee:GetChildren()
local chosenloadout1 = loadout1[math.random(1,#loadout1)]
chosenloadout1:Clone().Parent = player.Backpack

local loadout2 = replicatedstorage.storage.ranged:GetChildren()
local chosenloadout2 = loadout2[math.random(1,#loadout2)]
chosenloadout2:Clone().Parent = player.Backpack

local loadout3 = replicatedstorage.storage.misc:GetChildren()
local chosenloadout3 = loadout3[math.random(1,#loadout3)]
chosenloadout3:Clone().Parent = player.Backpack

Loadout’s folders location:
image

First off, please DRY!
Consider making the common task to parent a random loadout a function:

local function giveRandomLoadout(loadouts: Folder)
    local children = loadouts:GetChildren()
    children[math.random(1, #children)]:Clone().Parent = player.Backpack
end

Secondly, the issue at hand is likely due to FE. This seems to be a LocalScript, but it really should be a ServerScript. Consider transitioning your code to where the client requests the loadout, and the server actually parents the random loadout to them.

1 Like