Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

String expected, got Object error message?

I am made a chat command which gives the item a player is holding but the code keeps giving this error message, I'm guessing it is the backpack but I don't know for sure how to fix it, any help? (Btw I am using a server script)

local Players = game:GetService("Players")
local Prefix = ";"
local GroupId = 5059774


function GetPlayer(targetedPlayerName)
      for _,player in pairs(game:GetService("Players"):GetPlayers()) do
            if targetedPlayerName:lower() == player.Name:sub(1,targetedPlayerName:len()):lower() then
                return player 
            end
        end
        return nil 
end

Players.PlayerAdded:Connect(function(Player)
	if Player:GetRankInGroup(GroupId) >= 249 then
		Player.Chatted:Connect(function(msg)
			
			if msg:sub(1,11) == Prefix.."AddPoints " or msg:sub(1,11) == Prefix.."addpoints " then
				
				local Amount = msg:sub(12,13) or msg.sub(12,12)
				if msg:sub(15) == "me" or msg:sub(14) == "me" then
					Player.leaderstats["Cafe Points"].Value = Player.leaderstats["Cafe Points"].Value + Amount
				else
					local Target = GetPlayer(msg:sub(15)) or GetPlayer(msg:sub(14))
					Target.leaderstats["Cafe Points"].Value = Target.leaderstats["Cafe Points"].Value + Amount
				end
			end
			
			if msg:sub(1,10) == Prefix.."GiveItem " or msg:sub(1,10) == Prefix.."giveitem " then
				local Target = GetPlayer(msg:sub(11))
				for _,v in pairs(workspace[Player.Name]:GetChildren()) do
					if v:IsA("Tool") then
						local Thing = v.Name
						print(Thing)
						local Item = game.Lighting[Thing]:Clone()
						print(Item.Name)
						print(Target)
						print(Players[Target].Name) 
					end
				end
			end
		end)
	end
end)

Full error message: ServerScriptService.ServerScripts.CustomCmds:39: bad argument #2 to '?' (string expected, got Object)

6 comments

0

Can you give us the entire error message you're getting?

0

Updated

0

The error you're getting is on Line 39 of CustomCmds, could you indicate which line that is?

0

yeah we dont know what line 39 is.

0

Put the whole script in since I guess it would be a bit more helpful to you guys

0

Try removing print(Target), see what happens

1 answer

✓ Accepted answer

Well you didnt specify where line 39 is so i am going off where you said it might be coming from. I use :FindFirstChild() instead of doing what you are doing so this is how i would do what you are doing.

if msg:sub(1,10) == Prefix.."GiveItem " or msg:sub(1,10) == Prefix.."giveitem " then
    local Target = GetPlayer(msg:sub(11)) -- I dont know what this function does so ima leave it like this
    for _,v in pairs(workspace:FindFirstChild(Player.Name):GetChildren()) do
        if v:IsA("Tool") then
        	local Thing = v.Name
        	print(Thing)
        	local Item = game.Lighting:WaitForChild(Thing):Clone()
        	print(Item.Name)
        	print(Target)
        	print(Players:FindFirstChild(Target.Name).Name)
	end
end

this probably wont fix this problem but i hope I helped.

1 comment

0

I fixed it lol, I'm such an idiot.

This discussion is preserved as it was. New answers, comments, and votes are closed.