Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I create a working script that gives players guns during a round?

I created a script that would check that would give a player two random guns in game, because the main script only gives players guns once the round starts. There would be a problem where players did not have a gun if they joined after the round started, and they had to wait until the next round to get one. I created a script to fix this issue, which is down below, but I don't know what exactly is wrong with it.
InProgress is controlled by a seperate script which controls the map changing.

This is a simple script overall, but to my knowledge it should work. Any help would be great.

local replicatedstorage = game:GetService("ReplicatedStorage")
local status = replicatedstorage:FindFirstChild("InProgress")    -- InProgress is a value to determine if 			 	there is a game going on or not

while true do
	wait(.1)
	 if status.Value == 'Game'  -- InProgress is changed to this once a round begins
		then
			while true do
				function onPlayerEntered(newPlayer)
		
				for _, player in pairs(game.Players:GetPlayers()) do 
				local randomgun = game.ReplicatedStorage:FindFirstChild('Pri'):GetChildren()
				local gun1 = randomgun[math.random(1, #randomgun)]
				gun1:Clone().Parent = player.Backpack
				gun1:Clone().Parent = player.StarterGear
				local randomgun2 = game.ReplicatedStorage:FindFirstChild('Sec'):GetChildren()
				local gun2 = randomgun2[math.random(1, #randomgun2)]
				gun2:Clone().Parent = player.Backpack	
				gun2:Clone().Parent = player.StarterGear
				script.Sound:Play()	   -- this would usually help me to identify if the script went through or not
				wait(.5)
				end
			end
			end	
	else
		if
			status.Value == 'Lobby' -- Inprogress is changed back to this when the round is over, and the players are in the lobby
			then 
			wait(3)
		end
		end
end

1 answer

I'm not sure if this could be the case but, you have to wait for the character and player to load since they just joined the game so make sure its not equal to nil!

local replicatedstorage = game:GetService("ReplicatedStorage")
local status = replicatedstorage:FindFirstChild("InProgress")    -- InProgress is a value to determine if               there is a game going on or not

while true do
    wait(.1)
     if status.Value == 'Game'  -- InProgress is changed to this once a round begins
        then
            while true do
                function onPlayerEntered(newPlayer)

                for _, player in pairs(game.Players:GetPlayers()) do 
		if Player ~= nil and Player.Character ~= nil then
                local randomgun = game.ReplicatedStorage:FindFirstChild('Pri'):GetChildren()
                local gun1 = randomgun[math.random(1, #randomgun)]
                gun1:Clone().Parent = player.Backpack
                gun1:Clone().Parent = player.StarterGear
                local randomgun2 = game.ReplicatedStorage:FindFirstChild('Sec'):GetChildren()
                local gun2 = randomgun2[math.random(1, #randomgun2)]
                gun2:Clone().Parent = player.Backpack   
                gun2:Clone().Parent = player.StarterGear
                script.Sound:Play()    -- this would usually help me to identify if the script went through or not
                wait(.5)
                end
            end
            end 
    else
        if
            status.Value == 'Lobby' -- Inprogress is changed back to this when the round is over, and the players are in the lobby
            then 
            wait(3)
        end
        end
end
end



This discussion is preserved as it was. New answers, comments, and votes are closed.