Hello! With a friend we have been trying to do a group about roleplaying with police, but we need help about this, Giving a especific tool for a rank, example: a gun and another tool for the Police rank but not for the Recruit rank. And so go on. We have tried looking up for tutorials but they don’t seem go well, like Guns stacking up and not going for only 1 rank. please help! That code is down below
We tried this code
ocal GroupId = 00000 - Already put on the script this is an example.
local Tool = game.Lighting[“Gun”] - Example
local Tool = game.Lighting[“Gun”] - Example
function onPlayerSpawned(Player)
if Player:GetRoleInGroup(GroupId) then
Tool:Clone().Parent = Player.Backpack
end
end
Tried doing the same thing, i gave to my group rank aka 26, a “G17” gun, then to a lower rank which is 2, the same tool “G17” gun but i still get it on my inventory both guns?
local Players = game:GetService("Players")
local GrpId = 00000
local PoliceRank = 20
local RecruitRank = 10
local Baton = game.ServerStorage["Baton"]
local Gun = game.ServerStorage["Gun"]
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function()
if Player:GetRankInGroup(GrpId) == PoliceRank then
Gun:Clone().Parent = Player.Backpack
Baton:Clone().Parent = Player.Backpack
elseif Player:GetRankInGroup(GrpId) == RecruitRank then
Baton:Clone().Parent = Player.Backpack
end
end)
end)
` `` Before and after the script. Remove the space between them.
Well, if you only want one or the latter, you have to run elseif checks, from the higher to lower ranks.
An example would be:
local Rank = Player:GetRankInGroup(GroudId)
if Rank >= 100 then
-- Code who runs just for this
elseif Rank >= 50 then
-- Code who runs just for this
elseif Rank >= 25 then
-- Code who runs just for this
end
I reckon that there is a much more efficient way if you have a lot of possible loadouts, but for 2 or 3 this should be OK.
As previously stated, do not use Lighting for storage. Use a dedicated storage container, like ServerStorage (which will make the tools mostly inaccessible to exploiters).
If I was doing this, I’d probably structure my storage like so:
ServerStorage
GroupTools<Folder>
RankId<Folder>
Tool
Tool
And in my server script:
local Players = game:GetService("Players")
local GroupTools = game:GetService("ServerStorage").GroupTools
local GroupId = 1234567890
local function GiveGroupTools(player, groupId)
local rank = player:GetRankInGroup(groupId)
-- if they're not in the group, we don't need to do anything else
if not rank or rank == 0 then
return
end
-- loop through each of the rank folders
for _, toolFolder in ipairs(GroupTools:GetChildren()) do
local folderRank = tonumber(toolFolder.Name)
-- if the player's rank is less than or equal to this folder's targeted rank...
if folderRank <= rank then
-- loop through each tool within the folder
for _, tool in ipairs(toolFolder:GetChildren()) do
-- copy the tool into the player's backpack
local toolCopy = tool:Clone()
toolCopy.Parent = player.Backpack
end
end
end
end
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
-- wait for the player to spawn in, then give them the tools
GiveGroupTools(player, GroupId)
end)
end)
This way, each rank has a folder which you can store tools, and each higher rank will get its own tools as well as any lower ranks tools.
And if you wanted a certain tool to be exclusive to a rank, you could place a BoolValue (or any instance really) inside the tool, name it “RankExclusive,” and only clone the tool if the player’s rank is equal to that folder’s rank.