Colyseus problem with sending data (SOLVED)

Hello Defolders!(I don’t know how to call you :smiley:)

Long time no see! Because of the recent global events I have some free time to spare and I have an idea for a multiplayer prototype. I’m using Colyseus. I set up everything. It works! I try to send data to my localhost and it receives it… everything is okay. But then I glance to my Defold console and there was an error :

ERROR:SCRIPT: /assets/client.script:25: attempt to index upvalue ‘room’ (a nil value)
stack traceback:
/assets/client.script:25: in function </assets/client.script:23>

I’m sending a plain string ( room:send("word"))… and I don’t think a string is a nil value. I’m still new to Lua!

There may be a problem with the room. How do you connect to it? Is the room successfully created?

I think so. When I check out the Colyseus monitoring system, it lists the room id and everything. And I’m using the examples.

Can you send a simple example? I will try to help tomorrow

Yeah, I can provide you with anything you need. I mean the code works but it bugs me out every time i get the error. I’ve just attached this simple example I found on the “Getting started” section of the Colyseus docs to a Game Object in the main collection. I’m just in the beginning of the prototype and I don’t have much except a Platypus driven sprite.

Here is my code:

local ColyseusClient = require "colyseus.client"
 
 local client
 local room
 
 function init(self)
 client = ColyseusClient.new("ws://localhost:2567")
 client:join_or_create("chat", {}, function(err, _room)
 		if err then
 			print("JOIN ERROR: " .. err)
 			return
 		end
 
 		room = _room
 	end)
 end
 
 function update(self, dt)
 	client:loop()
 	room:send("word")
 end

Perhaps the room is created asynchronously. Try

if room then 
    room:send("word")
end
1 Like

Yes, that is definitely it. The room will not be created on the first frame and the update() function will obviously be called immediately from application start. Checking that room is not nil is the correct solution to avoid the problem.

1 Like

i am using the same code … but … how do i get my sessionID?? …

edit: the if room check did the trick here too …