How would I add a kill count to my game?

Godot Version
v4.3

Question

How would I add a kill counter in my game that’s multiplayer , Right now it shows at the screen of the player that was killed

it does this

@rpc("any_peer")
func receive_damage():
	health -= 1
	if health <= 0:
		health = 3
		position = Vector3.ZERO
		kill_count.text = "Kill count = " + str(score)
	health_changed.emit(health)

Have you tried using a singeton which can be used for global variables with a menu script or something along those lines, and changing using it there? Maybe something like that could work for you.
https://docs.godotengine.org/en/4.x/tutorials/scripting/singletons_autoload.html

2 Likes

Thanks

I found a another thread which I think it uses it,
however I’m getting an error
res://Player.gd:108 - Parse Error: Expected opening “(” after function name.

Here’s the code

@rpc("any_peer")
func Player.receive_damage.rpc(self):
	health -= 1
	if health <= 0:
		health = 3
		position = Vector3.ZERO
		kill_count.text = "Kill count = " + str(score)
	health_changed.emit(health)

@pesvoh
This isn’t related to your problem, but you should format your code so it’s easier to read.

Here’s how: (Not mine, but it works) :heavy_check_mark:

There is space after your function instead of parentheses and it also could it be that there is nothing after the if health<= 0:?

@rpc("any_peer")
func Player.receive_damage.rpc(self)
	health -= 1
	if health <= 0:
		health = 3
		position = Vector3.ZERO
		kill_count.text = "Kill count = " + str(score)
	health_changed.emit(health)

Here is how it’s meant to look

1 Like

So is the code
func Player.receive_damage.rpc(self)
meant to be calling a func or is that the code below it?

It’s meant to be calling a func I think ,
if you need more code I can send it

Your code below it should not be indented if the func line was not the function itself. If it is supposed to be that then it should look like func Player.receive_damage.rpc(self): and you should use the self “variable” or just delete it in case it gives you any popups.

Also I think it might not work if the function is labeled weirdly like that

I don’t know,
in this form Hi so how Would I be able to make an kill counter - #5 by Ragingburger
Do you know that they would be using to define the player?

I’m not 100% sure what your asking but if your asking on how you could get the player that killed the player then you could add a “variable” to the “player damage” function like:

func playerDamage(Player, Damage):

So when you call it you put the player and the damage in the function so it’s easy to work from there as you would have the killer/last person to damage.

Where did they reference player?

player.receive_damage.rpc(self)

I’m not sure, maybe it’s self or you might need to add it yourself

Also you need to add the semicolon at the end of the function I belive

How do you reference your player ? what methods should I use?

Could you explain what you mean a little bit more please?

I Mean when they reference player in this line on code

player.receive_damage.rpc(self)

Do you have an idea of how they referenced it?

I do not. From what. I have seen self does not do anything unless this func does anything important somewhere else in your code