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

Error 'invalid argument #1 to 'len' (string expected, got nil)', any help? [SOLVED]

Asked by 2 years ago
Edited 2 years ago

Hello, and thank you for reading! This is our client:

Client:

local player = game.Players.LocalPlayer
local CreateDialogEvent = game.ReplicatedStorage.CreateDialogEvent
local DialogFrame = player.PlayerGui.DialogGUI:WaitForChild("DialogFrame")
local DialogGUI = DialogFrame.Parent
local DialogText = DialogFrame:WaitForChild("DialogText")
local OriginalSound = script:WaitForChild("Talk")
local TEXT_INTERVAL = 0.075

local function playSound(soundID)
    local Sound = OriginalSound:Clone()
    Sound:Destroy()
end

local function Talk(Text)
    for i = 1, string.len(Text) do
        DialogText.Text = string.sub(Text,1,i)
        playSound()
        if string.sub(Text,i,i) == "." or string.sub(Text,i,i) == "!" or string.sub(Text,i,i) == "?" then
            wait(.5)
        elseif string.sub(Text,i,i) == "," then
            wait(.1)
        else
            wait(TEXT_INTERVAL)
        end
    end
end

CreateDialogEvent.OnClientEvent:Connect(function(imageID,Text)
    if not player:FindFirstChild("secretEnding") then
        DialogFrame.DialogImage.Image = imageID
        DialogText = ""
        DialogGUI.Enabled = true
        Talk(Text)
    end
end)

It is located in StarterCharacterScripts.

Now, I understand this error: Text is nil, as it doesnt exist, BUT, I have no other way to access it! There is a RemoteEvent inside ReplicatedStorage called CreateDialogEvent.

Server:

local Sound = script:WaitForChild("Talk")
local TEXT_INTERVAL = 0.03
local CreateDialogEvent = game.ReplicatedStorage.CreateDialogEvent

local randomPlayerName
local randomPlayerID

local function getPlayerImage(playerID)
    local content,isReady = game:GetService("Players"):GetUserThumbnailAsync(playerID,Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
    return content
end

local function getRandomPlayers()
    local Players = game.Players:GetPlayers()
    local number = math.random(1,#Players)
    local randomPlayer = Players[number]
    randomPlayerName = randomPlayer.Name
    randomPlayerID = randomPlayer.UserId
end

local function challengingExample()
    local Cab_Image = "rbxassetid://6927695957"
    CreateDialogEvent:FireAllClients(Cab_Image,"Dialog")
    wait(5)
    getRandomPlayers()
    CreateDialogEvent:FireAllClients(getPlayerImage(randomPlayerID,"Dialog 2"))
end

challengingExample()

-- Ignore the rest, I used this in the past but its here just incase.
--Talk("Loading...")
--wait(10)
--Talk("Urghh...")
--wait(6)
--Talk("Where are we?")
--wait(8)
--Talk("CAB!!!")
--wait(4)
--Talk("...")
--wait(4)
--Talk("We have to find help!")

So, that about wraps things up.

Thank you for reading, and, as always, any further questions can be answered politely!

0
try replacing string.len(Text) with #Text. Should work greatneil80 2647 — 2y
0
at quick glance it looks like you may not realise on line 26 of the server script the "Dialog 2" string is being sent to the getPlayerImage function and not the remote event enzotinman1 23 — 2y
0
i read the first script only, rip. ur probably right though greatneil80 2647 — 2y
0
Thank you for both of your help! I will try this now... LikeableEmmec 470 — 2y
0
Thank you! This worked!!! LikeableEmmec 470 — 2y

Answer this question