Command to assign a string value to leaderstats

So, I’m having some trouble figuring out how to make this happen but basically I’d want the command to look something like “/assign (user) (role/the string value)”.

image

The role or the string value will then get assigned to this. If you can help with this, please do. I appreciate it! <3

OnSwitchKill = true 

function commands(msg, recipient, speaker) 
	N = 0  -- This is needed!
	if string.sub(msg:lower(),1,5) == "join/" then 
		print(speaker.Name.. "activated \"Join\" command")
		a = game:service("Teams"):GetChildren() 
		local m = Instance.new("Message") 
		for i = 1, #a do 
			if string.sub(msg:lower(),6) == a[i].Name:lower() then 
				if speaker.TeamColor ~= a[i].TeamColor then 
					speaker.TeamColor = a[i].TeamColor
					print(speaker.Name.." joined new team | New team: "..a[i].Name) 
					if OnSwitchKill then 
						speaker.Character["Humanoid"].Health = 0
					end 
					m.Parent = speaker
					m.Text = "Team joined: "..a[i].Name
					wait(3)
					m.Parent = nil 
				elseif speaker.TeamColor == a[i].TeamColor then 
					print(speaker.Name.." attempted to join team "..a[i].Name.." denied: "..speaker.Name.." already on this team")
					m.Parent = speaker
					m.Text = "You are already on this team!"
					wait(3) 
					m.Parent = nil 
				end 
			elseif string.sub(msg:lower(),6) ~= a[i].Name:lower() then 
				N = N +1 
				if N >= #a then 
					N = 0 
					print(speaker.Name.." tried to join team \""..string.sub(msg,6).."\" failed: No such team exists within server")
					m.Parent = speaker
					m.Text = "\""..string.sub(msg,6).."\" is not a team!" 
					wait(3)
					m.Parent = nil 
				end 
			end 
		end 
	end 
end 




function findVoice(player) 
	player.Chatted:connect(function(msg, recipient) commands(msg, recipient, player) end) 
end 
game.Players.ChildAdded:connect(findVoice) 

This should work, make sure to credit Pieshop97 I do not intend to steal work, I want you to learn from this

Correct me if I’m wrong but is that not a script for a command to switch teams?

Hi!
I just wrote this code to help you with your issue:

game.Players.PlayerAdded:Connect(function(player)
	--// Create leaderboard
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	--// Add role to leaderboard
	local roleVal = Instance.new("StringValue")
	roleVal.Name = "Role"
	roleVal.Parent = leaderstats
	
	player.Chatted:Connect(function(msg)
		--// Check if /role was involved in the message
		if string.sub(msg, 1, 5) == "/role" and #string.split(msg, " ") >= 3 then
			local playerName = string.split(msg, " ")[2]
			local role = ""
			
			--// Validate player name
			local validPlayerName
			for _, player in next, game.Players:GetPlayers() do
				if playerName == player.Name then
					validPlayerName = true
				end
			end
			if not validPlayerName then return end
			
			--// Get role name
			for i, str in next, string.split(msg, " ") do
				if i ~= 1 and i ~= 2 then
					if i > 3 then role = role.." " end -- Add spaces
					role = role..str
				end
			end
			
			--// Set role using arguments from command
			if playerName and role ~= "" then
				game.Players[playerName]:WaitForChild("leaderstats"):WaitForChild("Role").Value = role
			end
		end
	end)
end)

Here is the result:

If this works for you, please respond with a like or solution marker. Otherwise, let me know what else you need!

Yup, it works great! I’ll be sure to let you know if something arises.