Attempt to get lenght of a instance value

I’m trying to make a command that punishes a player with a yellow card using a command.
Code:

		Function = function(plr,args)
			local ReplicatedStorage = game:GetService('ReplicatedStorage')
			local YCEvent = ReplicatedStorage:WaitForChild("PunishYC")
			local notifyEvent = ReplicatedStorage:WaitForChild("NotificationEvent")
			if not args[1] then return end
			args[2] = args[2] or ""
			local name = args[1]

			for i,v in pairs(game:GetService("Players"):GetPlayers()) do
				if v.Name:sub(1,#name):lower() == name:lower() then --error here
					YCEvent:FireClient(v,args[2])
					notifyEvent:FireAllClients("",true,v.Name..' got a yellow card. '..workspace.Scoreboard.Timer.Minutes.Value..':'..workspace.Scoreboard.Timer.Seconds.Value,true,"yellow")
					break
				end
			end
		end

But when running the command (using HD admin to run it as a custom command) it errors as:

- attempt to get length of a Instance value

And i’m unsure what causes it exactly, i’ve tried looking for solutions on the wiki and on the devforum without any results, i’m sure it’s some small mistake i just can’t seem to find where it is.

“name” is probably not a string, try putting this after the name variable and see what it prints

print(type(name))
2 Likes

Screenshot_270
yup, it’s outputting as userdata for some reason, it’s supposed to be a string (the player’s name while using the command :yellowcard (player))

That’s odd. Let’s analyze this:
v.Name:sub(1,#name):lower() == name:lower()

Perhaps it is an instance of some sort, what’s being fired on the function?

1 Like

args[1] would be the player’s username, args[2] would be the message after the player’s name so, :yc (player)(string), the fire event shows a GUI to the player who got carded and then proceeds to print it to a log, i tried using another admin system which supports custom commands (Adonis) and the command works fine ,seems to be an issue with HD admin somehow?

Maybe it is returning the player object itself? Try to change local name = args[1] line to local name = args[1].Name

try using strings values instead of instance value, not sure why but i’ve had trouble with them, then changed to string values and it worked

1 Like

Could you show the args table, in your command template?

I think you may be using a player object as an argument in the args table, so the first argument is actually a Player class instead of the name, which would mean you wouldn’t need the loop.

The error would make sense then, as you are trying to get the length of an Instance.

2 Likes

Thanks @guidable and @xVin_y changing the args from (“Player”, “String”) to (“string”, “reason”) fixed my issue.

The solution i stated does work, but it made the second argument duplicate itself (reason), changing the code to:

		Function = function(plr,args)
			local ReplicatedStorage = game:GetService('ReplicatedStorage')
			local YCEvent = ReplicatedStorage:WaitForChild("YCEvent")
			local notifyEvent = ReplicatedStorage:WaitForChild("NotificationEvent")

			for i,v in pairs(game:GetService("Players"):GetPlayers()) do				
					YCEvent:FireClient(v,args[2])
					notifyEvent:FireAllClients("",true,v.Name..' got a yellow card. '..workspace.Scoreboard.Timer.Minutes.Value..':'..workspace.Scoreboard.Timer.Seconds.Value,true,"yellow")
					break
				end
			end
	};	

fixed that issue, thanks for the help though!