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

(line 87) Expected string, got userdata? but that is a string!

Asked by 4 years ago

I'm trying to make a message command for when you say lib.cmds:msg.hi and it'd say "[lib] hi" but instead it errors out. Please help!

-- Made by iiDk
-- Settings
local version = "1"
local plr = owner -- aka iiDkOffical, i like owner more lol
-- Functions
local function message(text) -- Sends a message to the server
local msg = Instance.new("Message")
msg.Parent = game:GetService("Workspace")
msg.Text = text
wait(3)
msg:Destroy()
end
local function playSound(soundCat,soundName,soundId,musicId) -- Plays a sound
 local sound = Instance.new("Sound")
 sound.SoundId = "rbxassetid://"..soundId
 sound.MaxDistance = "99999999999999999999999999999999999999"
 sound.Volume = 1
 sound.Parent = game:GetService("Workspace").Base
 sound.Name = "lib.musicplayer"
 sound.Looped = true
 sound:Play()
 message("[lib] Now playing "..soundCat..": "..soundName.." (ID: "..soundId.." libID: "..musicId..")")
end
-- Main Commands
plr.Chatted:Connect(function(msg)
 if string.sub(msg,1,4) == "lib." then -- Finds main library
   if string.sub(msg,5,16) == "musicplayer:" then -- Makes sure if it's music player
    if string.sub(msg,17,21) == "play." then
     local assetname = game:GetService("MarketplaceService"):GetProductInfo(string.sub(msg,22))
     playSound("Custom",assetname.Name,string.sub(msg,22),"0")
    else
    if string.sub(msg,17,20) == "stop" then
local success, message = pcall(function()
    --Top
    game:GetService("Workspace").Base["lib.musicplayer"]:Destroy()
    message("[lib] Music stopped")
    --Bottom
end)
if not success then
    local msg = Instance.new("Message")
    msg.Parent = game:GetService("Workspace")
    msg.Text = "[lib] No music playing"
    wait(3)
    msg:Destroy()
end

    end
    end
   else
    if string.sub(msg,5,8) == "help" then
   message("lib.help: lib.musicplayer:play.id - plays the sound id specified | lib.musicplayer:stop - stops sounds playing | lib.help - shows commands")
else
if string.sub(msg,5,9) == "cmds:" then
if string.sub(msg,10,14) == "kill." then
local success, message = pcall(function()
--top
if game:GetService("Players"):FindFirstChild(string.sub(msg,15)).Character.Humanoid then
game:GetService("Players"):FindFirstChild(string.sub(msg,15)).Character.Humanoid.Health = 0
end
--bot
end)
if not success then
    local msg = Instance.new("Message")
    msg.Parent = game:GetService("Workspace")
    msg.Text = "[lib] No player found. Try their full username."
    wait(3)
    msg:Destroy()
end
else
if string.sub(msg,10,12) == "re." then
local success, message = pcall(function()
--top
game:GetService("Players"):FindFirstChild(string.sub(msg,13)):LoadCharacter()
--bot
end)
if not success then
    local msg = Instance.new("Message")
    msg.Parent = game:GetService("Workspace")
    msg.Text = "[lib] No player found. Try their full username."
    wait(3)
    msg:Destroy()
end
else
if string.sub(msg,10,13) == "msg." then
local msg = Instance.new("Message")
    msg.Parent = game:GetService("Workspace")
    msg.Text = "[lib] "..string.sub(msg,14)
    wait(3)
    msg:Destroy()
end
end
   end
 end
end
end
end
end)
message("[lib] Script has been loaded! "..plr.Name.." has libraryCommands. Say lib.help for help. ")
message("[lib] Note: you can't do anything bad so don't even think about it. Version "..version)

2 answers

Log in to vote
1
Answered by 4 years ago

You are setting msg to a new instance on line 85, so it is not a string by the time it gets to line 87.

0
no i'm trying to set the message of it iiDkOffical 109 — 4y
1
By setting msg=Instance.new("Message"), you overwrite the string that it used to be. On line 87, you call string.sub(msg,14), but you just set msg to something that is not a string, so it wont work. IStarConquestI 414 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

To manipulate any string, you need to make sure it's actually a string. You said it is but if you go back to that line, the message is an instance so if you try to "sub" an instance, it won't work.

I think you forgot to do msg.Text but instead you did msg alone.

To fix it, change this line in Line 87:

msg.Text = "[lib] "..string.sub(msg,14)

To this

msg.Text = "[lib] "..string.sub(msg.Text,14)

Answer this question