So, i'm not very good at all with scripting. I'm trying to make it to where when a player spawns, they receive a random weapon from a 'Weapons' folder in ReplicatedStorage. I have a baseline down, but I don't know how to actually clone the weapons over to the player's backpack each time they (re)spawn.
while true do do math.random(1, 2) if 1 then game.ReplicatedStorage.Weapons["Tool #1"]:clone().Parent = game.Players.LocalPlayer.Backpack end if 2 then game.ReplicatedStorage.Weapons["Tool #2"]:clone().Parent = game.Players.LocalPlayer.Backpack end end
It is a localscript in ServerScriptService. Lemme know what I need to do to make it work. Tell me what i've done wrong, etc. I want to learn.
Thanks.
Alright this script can use a lot of optimizing. I'll try my best to explain along the way.
-- place this as a localscript inside of StarterPlayer or startergui local weapons = {game.ReplicatedStorage.Weapons["Tool #1"], game.ReplicatedStorage.Weapons["Tool #2"]} -- add more weapons/tools here. repeat -- this loop repeats until a condition is met wait(0.1) -- dont want to lag the client, so we add a wait until nil ~= game.Players.LocalPlayer.Character -- Character has spawned local selected = math.random(1, #weapons) -- Pick 1 of weapons at random selected:Clone().Parent = game.Players.LocalPlayer.BackPack -- Clone it and give it to the player script:Destroy() -- We dont need this script anymore. it wont delete from startercharacter, but from the spawned players character thus saving memory and reducing lag.
Make sure you move the localscript from serverscriptservice to starterchracter.
So I am not an expert but here goes
if I were to do something like this I would first scan for when a player is added to the game, randomly choose their weapon and then save the choice in the server:
local players = game:GetService("Players") local serverscripts = game:GetService("ServerScriptService") local ReplicatedStorage = game:GetService("ReplicatedStorage") players.PlayerAdded:Connect(function(player) local choice = Instance.new("IntValue",serverscripts) choice.Name = player.UserId choice.Value = math.random(1,2) end)
then I would add a script that checks if a character has been added and give them their weapon
local players = game:GetService("Players") local serverscripts = game:GetService("ServerScriptService") local function onCharacterAdded(character) local player = Players:GetPlayerFromCharacter(character) local x = serverscripts:FindFirstChild(player.UserId).Value if x == 1 then local weapon = game.ReplicatedStorage.Weapons["Tool #1"]:clone() weapon.Parent = player.Backpack end if x == 2 then local weapon = game.ReplicatedStorage.Weapons["Tool #2"]:clone() weapon.Parent = player.Backpack end end local function onPlayerAdded(player) player.CharacterAdded:Connect(onCharacterAdded) player.CharacterRemoving:Connect(onCharacterRemoving) end players.PlayerAdded:Connect(onPlayerAdded)
Fair Warning, I have not tested these scripts!
you can get some info in here:
https://developer.roblox.com/en-us/api-reference/event/Player/CharacterAdded
https://developer.roblox.com/en-us/api-reference/event/Players/PlayerAdded
By the way, I think you may need to read this:
https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events