Help with leaderstats

Well, I want to make a game with classes in it, and there is a special class called “Lightinfantry”, but I only want to give it out to certain people. The default class is “Fusiliers”.

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats  = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"
	
	local  Class = Instance.new("IntValue", leaderstats)
	Class.Name = "Class"
	
	local Fusilier = "Fusilier"
	Class.Value = Fusilier
	
	local Lightinfantry = "Lightinfantry"
	
end)

This is what I have, but how do I make it give LightInfantry to only certian people, and gives out weapons in the starter pack to only people with that role.

3 Likes

You can make a table containing of certain player’s UserId and then check if their UserId is in the table if it is then give them it.

1 Like

I don’t know how to do that…

  • You would make a table with a list of Id’s of certain users.
  • You would then loop through the table and compare if the players ID matches one of the Id:s on the table.
  • If the Id does match you would them give them the other value

Example

--// Services
local Players = game:GetService("Players")

--// Components
-- Create a table with Id's
local SpecialUsers = {1,5378439822} -- Just add user Id's of the users you want to have the Lightinfantry Rank. Separate new Id:s with commas

--//Handler
Players.PlayerAdded:Connect(function(player)

	local leaderstats  = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local  Class = Instance.new("IntValue", leaderstats)
	Class.Name = "Class"

	local Fusilier = "Fusilier"
	Class.Value = Fusilier
	
	--Loop through the SpecialUsers Table
	for i,v in pairs(SpecialUsers)do
		if(player.UserId == v)then -- If the players Id matches any Id in the table 
			local  LightinfantryClass = Instance.new("IntValue", leaderstats)
			Lightinfantry.Value = "Lightinfantry"
			-- Add this to them
		end
	end
end)
3 Likes

You should switch to ipairs() as it is used for arrays, SpecialUsers is an array.
pairs() is used for dictionaries mostly.

And, you can use table.find instead of looping through the array.
Example: Click here.

The last thing: Do NOT use the parent argument with Instance.new if you are assigning properties to them afterwards. Doing so is slower.

2 Likes
ServerScriptService.Leaderboard:22: attempt to index string with 'Value'

I get this in return??

That’s because you cannot assign a IntValue with a string.
If you want to have a string, you must rename IntValue to StringValue.

1 Like

Same thing happens. Any fix??? and it creates a new thing on the leaderboard just called value.

local Class = Instance.new("StringValue", leaderstats)
Class.Name = "Class"
if table.find(SpecialClassPlayers, plr.UserId) then

local Fusilier = "Fusilier"
Class.Value = Fusilier
else
print("plr UserId not found")
end
--// Components
-- Create a table with Id's
local SpecialUsers = {326555533, 406340380, 1779940452} -- Just add user Id's of the users you want to have the Lightinfantry Rank. Separate new Id:s with commas

--//Handler
Players.PlayerAdded:Connect(function(player)

	local leaderstats  = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local  Class = Instance.new("IntValue", leaderstats)
	Class.Name = "Class"
	local Fusilier = "Fusilier"
	Class.Value = Fusilier
	
	local Lightinfantry = "Lightinfantry"
	
	
	if table.find(SpecialUsers, player.UserId) then
	Class.Value = Lightinfantry
	else
		print("plr UserId not found")
	end
end)

This is what I have down, I probably did this wrong, but it doesn’t work.

local Players = game:GetService("Players")
--// Components
-- Create a table with Id's
local SpecialUsers = {326555533, 406340380, 1779940452} -- Just add user Id's of the users you want to have the Lightinfantry Rank. Separate new Id:s with commas

--//Handler
Players.PlayerAdded:Connect(function(player)

	local leaderstats  = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local  Class = Instance.new("StringValue", leaderstats)
	Class.Name = "Class"
	
	local Fusilier = "Fusilier"
	
	local Lightinfantry = "Lightinfantry"
	
	if table.find(SpecialUsers, player.UserId) then
	Class.Value = Lightinfantry
	else
		print("plr UserId not found")
		Class.Value = Fusilier
	end
end)

I just had to change it to string value. thx bois/girls.

2 Likes