How can i make this into a working script

i just wanna make a script that gives a random player in the server an ar-15

idk how to make it, i have somewhat an idea of it but its hard to do this so im super confused on it. it’s a main-part of my game

dont know any solutions so im coming here because im not good at scripting

local AR1 = game.ServerStorage["AR-15"]
local playercount = game.Players:GetChildren()
local random = (math.random(1, 2))

not to mention my games gonna have random players so i cant have it at a specific amount like ‘2’


script is in server script service
normal script
ar-15 is in serverstorage

1 Like

Just get a random player using game.Players:GetChildren()[math.random(1,#game.Players:GetChildren())] --wil return a random player and them copy the AR1 and parent put its parent as the player.

3 Likes

how do i implement this into my script

1 Like

To be easier to read:

local function GiveRandomPlayerAR()

    local randomPlayer = game.Players:GetChildren()[math.random(1,#game.Players:GetChildren())]
    local Model = game.GetStorage["AR-15"]:Clone()
    Model.Parent = randomPlayer

end

GiveRandomPlayerAR() -- calls the function

2 Likes

ServerScriptService.Script:3: invalid argument #2 to 'random' (interval is empty) - Server - Script:3
how do i fix this

1 Like

You’re probably firing it before the player joins the server. You could try adding an task.wait() before (I wrote something wrong too, the code below is working).

task.wait(2) -- wait for player to join
local function GiveRandomPlayerAR()
	
	local PlayerList = game.Players:GetChildren()
	local randomPlayer = PlayerList[math.random(1,#PlayerList)]
	local Model = game.ServerStorage["AR-15"]:Clone()
	Model.Parent = randomPlayer
	
	print(randomPlayer.Name.." was chosen!")
	
end

GiveRandomPlayerAR() -- calls the function
2 Likes

no errors but it didnt work and didnt print anything

1 Like

It might still be firing too soon.

adding a brick, connecting a .Touched event to it, and calling the function on touch so you can control when it is called.

2 Likes

how is touching a part gonna help… its a gun that is given to a random player that joins

1 Like

Yes. However, you’re currently trying to see if the function itself works.

Otherwise, add playerAdded functions and call the gun-giving function when the amount of players are over (whatever you want, use 1 for testing)

1 Like

i found out how:

local function GiveRandomPlayerAR()
	local PlayerList = game.Players:GetChildren()
	if #PlayerList > 0 then
		local randomPlayer = PlayerList[math.random(1, #PlayerList)]
		local Model = game.ServerStorage["AR-15"]:Clone()
		Model.Parent = randomPlayer.Backpack

		print(randomPlayer.Name .. " was chosen!")
	else
		warn("No players in the game to give the AR-15 to.")
	end
end

-- call the function when a player joins
game.Players.PlayerAdded:Connect(function()
	task.wait(2) -- wait for player to fully join
	GiveRandomPlayerAR()
end)
2 Likes

Every time that a player joins, it will give the AR to a random player (it can give it to someone that already have). Is that what you want?

3 Likes

nopeeeeeee but give me a second and i’ll try to fix it

1 Like
local hasGivenAR = false

local function GiveRandomPlayerAR()
    if hasGivenAR then
        return
    end

    local PlayerList = game.Players:GetChildren()
    if #PlayerList > 0 then
        local randomPlayer = PlayerList[math.random(1, #PlayerList)]
        local Model = game.ServerStorage["AR-15"]:Clone()
        Model.Parent = randomPlayer.Backpack
        
        print(randomPlayer.Name .. " was chosen and given an AR-15!")
        hasGivenAR = true
    else
        warn("No players in the game to give the AR-15 to.")
    end
end

-- Call the function when a player joins
game.Players.PlayerAdded:Connect(function()
    task.wait(2) -- wait for player to fully join
    GiveRandomPlayerAR()
end)

-- Initial call in case players are already in the game
task.wait(2) -- wait for initial players to join
GiveRandomPlayerAR()

is this good? or is it gonna not work?

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.