Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

String expected, got Object error message?

Asked by 4 years ago
Edited 4 years ago

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)

0
Can you give us the entire error message you're getting? plasmascreen 143 — 4y
0
Updated Microsoft_Net 21 — 4y
0
The error you're getting is on Line 39 of CustomCmds, could you indicate which line that is? plasmascreen 143 — 4y
0
yeah we dont know what line 39 is. Cald_fan 26 — 4y
View all comments (2 more)
0
Put the whole script in since I guess it would be a bit more helpful to you guys Microsoft_Net 21 — 4y
0
Try removing print(Target), see what happens plasmascreen 143 — 4y

1 answer

Log in to vote
1
Answered by 4 years ago

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.

0
I fixed it lol, I'm such an idiot. Microsoft_Net 21 — 4y
Ad

Answer this question