Help with award command

RobloxStudioBeta_cHpw0pKLIS

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		local args = msg:split(" ")
		local plruser = args[2]
		local user = game.Players:FindFirstChild(plruser)
		
		if args[1] == "-award" and plr:GetRankInGroup(33075577) >= 228 then
			if user and user.leaderstats.Exp then
				local currentValue = user.leaderstats.Exp.Value
				local fields = {
					{
						['name'] = "Info",
						['value'] = "User: " .. plr.Name .. "\nRank: **" .. plr:GetRoleInGroup(33075577) .. "**",
						['inline'] = true
					},
					{
						['name'] = "Award Info",
						['value'] = "User Awarded " .. user.Name .. "\nNow has **" .. currentValue .. "** EXP."
					}
				}
				
				currentValue += script.Parent.Variables.Awards.AwardToGive.Value
				print(" --[ Awards ]-- ")
				print("     Gave "..user.Name.. " "..script.Parent.Variables.Awards.AwardToGive.Value)
				print(" --[ Awards ]-- ")
				user:LoadCharacter()
				ws:createEmbed(url, "WARN", "This is published on 30/10/2023 11:08 pm by **RobloxSupp515**", fields)
			else
				print("Failed to award player, is it a correct user?")
			end
		end
	end)
end)

The script is erroring since Exp doesn’t exist. Use user.leaderstats:FindFirstChild("Exp") or user.leaderstats:WaitForChild("Exp") instead to prevent the error.

None of them work, Mind doing other steps?

Did the rest of the code not work or were they still erroring?
I wasn’t clear on my first post. The reason you’re getting errors is because Exp doesn’t exist. You need to make sure you’re actually creating it somewhere.

Only the eight line, if user and user.leaderstats.Exp then

Is there another script in your game where you’re creating an “Exp” value in the player’s leaderstats folder?

Yes I do, Is in ServerScriptService

If you need it here.

local datastore = game:GetService("DataStoreService")
local mainDS = datastore:GetDataStore("Stats")

game.Players.PlayerAdded:Connect(function(Player)
	-- Leaderstats --
	
	local leaderstats = Instance.new("Folder", Player)
	leaderstats.Name = "leaderstats"
	
	local Level = Instance.new("NumberValue", leaderstats)
	Level.Name = "Level"
	Level.Value= 1
	
	local Exp = Instance.new("NumberValue", leaderstats)
	Exp.Name = "Exp"
	Exp.Value= 0
	
	local TimeRank = Instance.new("StringValue", Player)
	TimeRank.Name = "TimeRank"
	TimeRank.Value = "N/A"
	
	local DutyTime = Instance.new("IntValue", Player)
	DutyTime.Name = "Duty"
	DutyTime.Value = 30
	
	local WarnValue = Instance.new("NumberValue", Player)
	WarnValue.Name = "Warns"
	WarnValue.Value = 0
	
	local RequiredExp = Instance.new("NumberValue",Player)
	RequiredExp.Name = "RequiredExp"
	RequiredExp.Value = Level.Value*150
	
	-- Level Up & Exp --
	
	local data
	local succ, err = pcall(function()
		data = mainDS:GetAsync(Player.UserId.."-stats")
	end)	
	
	if succ then
		if data then
			Level.Value = data[1]
			Exp.Value = data[2]
		end
	else
		print("Failed.")
		warn(err)
	end
	
	game.Players.PlayerRemoving:Connect(function(plr)
		local level = plr.leaderstats.Level.Value
		local exp = plr.leaderstats.Exp.Value
		
		local data = {level,exp}
		
		local succ,err = pcall(function()
			mainDS:SetAsync(plr.UserId.."-stats",data)
		end)
		
		if succ then
			print("[DATASTORE] - DataStore saved")
		else
			warn("[DATASTORE] - DataStore saving [FAILED]")
		end
		
	end)	
	
	while true do
		if Player:GetRankInGroup(33075577) >= 10 and Player:GetRankInGroup(33075577) <= 226 then
			wait(60)
			Exp.Value += 50
		elseif Player:GetRankInGroup(33075577) >= 288 then
			wait(60)
			Exp.Value += 80	
		elseif Player:GetRankInGroup(33075577) >= 228 and Player:GetRankInGroup(33075577) <= 255 then
			wait(60)
			Exp.Value += 100	
		end
	end
end)
1 Like

My bad, that all looks good.

Can you try printing args[2] and user to make sure they exist? If there isn’t a player by the name of args[2] then user will equal nil and the if block won’t run.

All it does is just print it. It just says “aaaaa” and when i do it with the correct user it does this. “RobloxSupp515”

nvm i got a new error here ServerScriptService.Cmds.Award:22: attempt to concatenate nil with string

Have you edited the script at all since posting the original? Would you mind posting what line 22 is now?

Well i deleted the findfirstchild now,

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		local args = msg:split(" ")
		local plruser = args[2]
		local user = game.Players:FindFirstChild(plruser)
		print(plruser)

		if args[1] == "-award" and plr:GetRankInGroup(33075577) >= 228 then
			if user then
				local currentValue = user.leaderstats:FindFirstChild("Exp")
				local fields = {
					{
						['name'] = "Info",
						['value'] = "User: " .. plr.Name .. "\nRank: **" .. plr:GetRoleInGroup(33075577) .. "**",
						['inline'] = true
					},
					{
						['name'] = "Award Info",
						['value'] = "User Awarded " .. user.Name .. "\nNow has **" .. currentValue .. "** EXP."
					}
				}

				currentValue += script.Parent.Variables.Awards.AwardToGive.Value
				print(" --[ Awards ]-- ")
				print("     Gave "..user.Name.. " "..script.Parent.Variables.Awards.AwardToGive.Value)
				print(" --[ Awards ]-- ")
				user:LoadCharacter()
				ws:createEmbed(url, "WARN", "This is published on 30/10/2023 11:08 pm by **RobloxSupp515**", fields)
			else
				print("Failed to award player, is it a correct user?")
			end
		end
	end)
end)

The 22nd line in that script is blank. Please click on the error in the output and paste the line that it takes you to.

Line 19

['value'] = "User Awarded " .. user.Name .. "\nNow has **" .. currentValue .. "** EXP."

That must be because currentValue is nil, which would be caused by user.leaderstats:FindFirstChild("Exp") returning nil, meaning Exp still doesn’t exist. I really don’t know why it doesn’t; from my testing your code is working fine. I’m sorry that I’m taking so much of your time