Error in Welcome code

I was trying to create a dialogue code for my game
and this error popped up, take a look at the code:

StarterPlayerScripts:

local currentEvent = game.ReplicatedStorage.customMessage

currentEvent.OnClientEvent:Connect(function(player: Player, text: string)
	local defaultLastTime = 10
	local ui = player.PlayerGui

	local accumulatedMsg = ""

	local length = string.len(text)

	for i = 1, length do
		local char = text:sub(i, i)
		if tonumber(char) then
			if accumulatedMsg ~= "" then
				print(accumulatedMsg)
			end
			ui.dialogueplr.txt.Text = char
			accumulatedMsg = ""
		else
			accumulatedMsg = accumulatedMsg .. char
		end
		wait(0.05)
	end

	if accumulatedMsg ~= "" then
		-- i will just leave this blank
	end

	wait(defaultLastTime)

	accumulatedMsg = ""
	for i = length, 1, -1 do
		local char = text:sub(i, i)
		if tonumber(char) then
			if accumulatedMsg ~= "" then
				print(accumulatedMsg)
			end
			ui.dialogueplr.txt.Text = char
			accumulatedMsg = ""
		else
			accumulatedMsg = char .. accumulatedMsg
		end
		wait(0.09)
	end
end)

ServerScriptService code:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		local SENDING = 'Welcome!'
		player:WaitForChild("PlayerGui",15)
		wait(0.02)
		
		game:GetService("ReplicatedStorage").customMessage:FireClient(player,SENDING)
	end)
end)

The only error i get is at the StarterPlayerScripts code, at line 10, it says: invalid argument #1 to ‘len’ (string expected, got nil) and if u saw the server code you could see how i am sending a string to the client but it keeps saying it’s nil.

Hey! When sending an event to the client from the server, the client argument “player” is actually the first argument sent over so your “SENDING” message is actually “player”. Hope this helps!

As you do not need to define a player on the client

Now the error is ‘Unable to cast value to Object’ when only sending the ‘SENDING’ argument.

Do you know what line that would be on by any chance?

In line 9 at the ServerScriptService code, i get the ‘Unable to cast value to Object’ error.

From what I’ve read I believe you’ve actually removed the “player” when you fire :FireClient() you need to keep both player argument and sending argument but on your actually client code where you have .OnClientEvent() you have player inside those arguments. You need to remove player from that!

My apologies if that’s the case ignore the above

player variable doesn’t get passed on client handler, in your case your arguments are:
player - “Welcome!”, text - nil

That fixed the issue, thanks guys!

No worries! Don’t forget to mark one of our replies as the solution to let other developers know that it’s been resolved!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.