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

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

Asked by 8 years ago

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

Log in to vote
0
Answered by 8 years ago

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



Ad

Answer this question