Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make a script that gives players a random weapon each time they spawn work properly?

Asked by 5 years ago

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.

0
I know you say you are bad at scripting, but why are you using two "do" in your script? I never seen this. Was that a mistake you made? SilverCreeper58 23 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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.

0
22:50:28.645 - Players.zhendirez.PlayerGui.LocalScript:9: attempt to index local 'selected' (a number value) It says that 'selected' needs to be a number value. Any ideas? zhendirez 2 — 5y
0
do i need to rename the guns "Tool #(the number)"?? MyPandaEyes -5 — 4y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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

0
Hello, I am currently trying to achieve this but I can't find out where you would put these scripts, I'm very new to scripting. FBS_8 25 — 5y

Answer this question