Random Team item giver

Hey,
I’m trying to make special forces team, and to make it more realistic, I want to give to each soldier a different weapon (M4A1, MP5, P90). I just don’t know how to make these kind of script, as long as I’m not very experienced with random in script.
Ik that we aren’t supposed to give full script, but I searched everywhere and i didn’t find anything, and I don’t know how to start the script, and what to put in.

1 Like

I would do something when a part is touched this script will run:

local Players = game:GetService("Players") -- Getting the player service
local Team = game:GetService("Teams")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local TeamPart = script.Parent

TeamPart.Touched:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent) 
	if not player and player.Team ~= nil then return end
	
	player.Team = Team["SFT"] --Special Forces Team
	
	-- Give Items | Lets say from replicatedStorage
	for _, weapon in pairs(ReplicatedStorage.WeaponsFolder:GetChildren()) do -- If all weapons are in a folder
		if not weapon:IsA("Model") then return end
		weapon:Clone().Parent = player.Backpack -- Cloning the weapon and parenting it to players backpack.
	end
end)

Alliteratively to be safer you could fire a remote Event to the server which would give the weapons. However I would look more into that yourself so you can understand it in your own way.

1 Like
local Players = game:GetService("Players") -- Getting the player service
local Team = game:GetService("Teams")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local TeamPart = script.Parent

TeamPart.Touched:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent) 
	if not player and player.Team ~= nil then return end
	
	if player.Team = Team["SFT"] then --Special Forces Team (REMEMBER: do if and then)
	for _, weapon in pairs(ReplicatedStorage.WeaponsFolder:GetChildren()) do -- If all weapons are in a folder
		if not weapon:IsA("Model") then return end
		weapon:Clone().Parent = player.Backpack -- Cloning the weapon and parenting it to players backpack.
	end
end)
1 Like

A way you could do that is to place the guns in folders for specific teams, like if you want a team to be able to spawn in with M4A1 or an MP5, put them in a folder and other teams with something else, put them i ntheir own folder. For this example, let’s say you placed them in folders called Enemies and Allies. In a script in somewhere like ServerScriptService

local playerserv = game.Players
local teams = game:GetService("Teams")

local allyWeapons = --Location of the Ally Weapons Folder
local enemyWeapons = --Location of the Enemy Weapons Folder

local allyTeams = {
	--Add your ally team names here
}

local enemyTeams = {
	--Add your enemy team names here
}

playerserv.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		local weaponfolder
		
		
		--Get folder depending on where your team is
		if table.find(allyTeams, plr.Team.Name) then
			weaponfolder = allyWeapons
		elseif table.find(enemyTeams, plr.Team.Name) then
			weaponfolder = enemyWeapons
		end
		
		--If we found a folder, meaning you're an Ally or enemy
		if weaponfolder then
			weaponfolder = weaponfolder:GetChildren()
			
			--Create random weapon
			local randomweapon = weaponfolder[math.random(weaponfolder)]
			
			--Add it to your backpack
			randomweapon:Clone().Parent = plr.Backpack
		end
	end)
end)

This code will put a clone of a random weapon for the right folder in your backpack when you spawn/respawn, should hopefully work how you want it too, you’ll have to add some stuff of course to make it work

2 Likes

@Black_Albi As @EmbatTheHybrid said to me before in another post, before posting, make sure to try finding a script, and if it’s do not work, then do a post.

1 Like

The only reason I didn’t add the if is because I’m not sure if your allowed to switch between Teams

1 Like

Problem is that I saw any script on the Internet.
Even on ytb videos

1 Like

I would recommend @EmbatTheHybrid method, however I would recommend learning tables before doing so as they are really useful and commonly used. I would recommend looking into teams as well, that way you can understand the code a bit better. Hopefully we helped you! Any questions feel free to ask.

2 Likes

Mobile is definitely fine.mp3

It’s actually not that difficult to make something relevant like that, you can make it work in about 3 simple steps here

Step 1: Parenting your tools in the correct spot

Making your tools should be the simple part if you’ve already know how to work with them, but what I would do is put them inside a folder & inside ServerStorage so that way exploiters aren’t capable of cloning the weapons multiple times (ServerStorage is only visible to the server)

Step 2: Making the code

Now this is where things and my sanity with formatting start to happen, we’re gonna first put the Script inside ServerScriptService so that way we know where to organize our scripts

The first thing we’ll do is go ahead & add a PlayerAdded event, which fires whenever a player joins the game:

game.Players.PlayerAdded:Connect(function(Player)  --This Player parameter will be what we'll use
end) 

Now if we want to give the player a tool every time we use a CharacterAdded Event :thinking: But what’s the difference between a PlayerAdded and CharacterAdded? Well:

  • PlayerAdded only fires whenever a Player Object is created & a Player joins the game, while
    CharacterAdded fires every time the Character Model gets added to the workspace, hence the name CharacterAdded

  • PlayerAdded will work within the game.Players function, while CharacterAdded will work when there is a Player object (Which is why we define the Player first inside the PlayerAdded event

Moving on, let’s say we wanted to add a CharacterAdded event onto our code here:

game.Players.PlayerAdded:Connect(function(Player)  --This Player parameter will be what we'll use
   Player.CharacterAdded:Connect(function(Character) --This fires every time the Character will be added
    end)
end) 

Next up, is creating a Random Tool for the Player to obtain every time they respawn! It shouldn’t be too bad, but we’ll go ahead and add a couple more stuff to our code here:

game.Players.PlayerAdded:Connect(function(Player)  --This Player parameter will be what we'll use

 
   Player.CharacterAdded:Connect(function(Character) --This fires every time the Character will be added
        local Weapons = game.ServerStorage.Weapons:GetChildren()
        local RandomWeapon = Weapons[math.random(1, #Weapons)]
        local WeaponClone = RandomWeapon:Clone()
        WeaponClone.Parent = Player.Backpack
    end)
end) 

Now we got a couple more things to keep in mind, but it’s pretty straightforward so I’ll go ahead and break it up:

local Weapons = game.ServerStorage.Weapons:GetChildren()

Is referring to where we put our Folder from, which is inside the ServerStorage & and with the GetChildren() function, we’re actually getting a table of all the weapons inside that folder

local RandomWeapon = Weapons[math.random(1, #Weapons)]
local WeaponClone = RandomWeapon:Clone()
WeaponClone.Parent = Player.Backpack
  • RandomWeapon is referring to the Weapons variable we defined earlier, & we’re using math.random to get 1 of the weapons from inside there

  • WeaponClone will create another Tool using the Clone() function, and we’re Parenting it to the Player's Backpack so they’re able to access it within the key they press

Whew, that was a lot of stuff! Now we’ll move onto the last step

Step 3: Testing out the Script

If you managed to get everything working right, then you should have an outcome of this:

  • The CharacterAdded Event will fire, giving the Player a random tool upon every respawn

  • The script should work for every individual player that joins

  • The ToolClone will be randomized & be accessed by every individual player

Then congrats you’ve made a basic Tool Randomizer Script!

If you just wanna skip to the actual code, then here it is!

game.Players.PlayerAdded:Connect(function(Player)  --This Player parameter will be what we'll use

   Player.CharacterAdded:Connect(function(Character) --This fires every time the Character will be added
        local Weapons = game.ServerStorage.Weapons:GetChildren()
        local RandomWeapon = Weapons[math.random(1, #Weapons)]
        local WeaponClone = RandomWeapon:Clone()
        WeaponClone.Parent = Player.Backpack
    end)
end) 

If you do have any questions do feel free to ask, I did my best with explaining it & this is on mobile so anyone do correct me if I missed anything :sweat_smile: