How do I fix the score counter? (SOLVED)

Please help. I’ve been trying to get this to work for two days. I am editing the code for the snake game and trying to make a scoreboard. When the snake finds tiles 2/3 it dies. When it finds tiles 5/6/8 I want the score to be updated.

snake.script code:


main.gui_script code:

files:

The game builds without errors, but the score remains fixed at 0.

I don’t see any obvious problem. Did you try some print debugging or the built in debugger to check if the main.gui_script actually receives the “add_score” message?

1 Like

Thank you! I’m trying it now.

Hello,

I tried debugging and it turns out that the script isn’t receiving the command. The score gets updated in the snake.script, but the message isn’t sent to the main.gui_script

Did you attach the main.gui_script to the main.gui?

I did, yes.

This script is doing:

msg.post("#main", "add_score", {amount = self.score})

The script expects that there is a component with id main on the same game object as the snake.script. I’m guessing that the main.gui isn’t attached to this game object?

I honestly don’t get it. I followed all the procedures (I think), but it doesn’t enter the “add” message.

gui script:

snake script:

Umm… I’m sorry, but I didn’t understand the attaching part. I’m really new at this. This is a screenshot of my files:

Ok, that looks right. The snake.scipt and the main.gui are both attached to the snake game object. The message really should be sent to the main.gui and the script. Can you pleas zip your project and share it here?

Oh, hold on. You have two main.gui! You have one on the main game object and one on the snake game object.

Remove the one on the snake game object and keep the one on the main game object. Change your msg.post() to:

msg.post("main#main", "add_score", ...)

I just did! The result is still the same though :frowning:

Should I send the file to you?

IT WORKED!

I had included this code in the main.gui_script file:

function on_message(self, message_id, message, sender)
if message_id == hash(“game_over”) then
msg.post(“gameover”, “enable”)
end
end

I understand that it’s wrong. I was trying to make a game over text appear when the snake collides with itself. When I removed this code, the point system worked.

Thank you so much for your help!!

Happy to hear that you managed to solve the problem!