Host is player taking all damage from players no mater what

Godot Version

4.3

Question

Hi I’m new on the forums so sorry if this is the wrong place, but I need help with this. I’m working on a fps and when ever I shoot its doing some real weird stuff. So when ever the “Host” player shoots another player nothing happens (not good) and if 2 non host players shoot each other nothing happens but if any other player shoots the host he takes the damage
I have no idea how this forum stuff works so I don’t know if i need like anything special in the post but uh heres the code for calling out a hit:


		if Input.is_action_pressed("fire"):
	# second shotgun
			if guntype == 6:
				if shot_2load > 0 && shot_02am.is_playing() == false:
					shot_2load = shot_2load-1
					hud.rounds = shot_2load
					shot_02am.queue("fire")
					if self.is_in_group("Team 1") == true:
						for r in shot_02cast.get_children():
							if r.is_colliding():
								if r.get_collider().is_in_group("Team 2"):
									r.get_collider().shot2hit.rpc_id(r.get_collider().get_multiplayer_authority())
									print("a player was shot")
					if self.is_in_group("Team 2") == true:
						for r in shot_02cast.get_children():
							if r.is_colliding():
								if r.get_collider().is_in_group("Team 1"):
									r.get_collider().shot2hit.rpc_id(r.get_collider().get_multiplayer_authority())
									print("a player was shot")

this is the shot function

@rpc("any_peer")
func shot2hit():
	var qty = 6
	subtracthealth(qty)
the part that subtracts health and  respawns you


func subtracthealth(qty):
	if $MultiplayerSynchronizer.get_multiplayer_authority() == multiplayer.get_unique_id():
		health = health - qty
		hud.health = health
		if health <= 0:
			hud.health = 0
			$Timer.start

func _on_timer_timeout() -> void:
	respawn()

func respawn():
	if $MultiplayerSynchronizer.get_multiplayer_authority() == multiplayer.get_unique_id():
		global_position = spawnpoint.global_position
		health = 100
		shotammo = 6
		shot_2load = 2
		guntype = 55

Any help would be greatly appreciated :)

From reading the code, here’s what I suspect might be the problem.

  • When someone gets shot, shot2hit gets called on the client of whoever got shot.
  • On that player’s client, subtracthealth() gets called.
  • The function first checks if $MultiplayerSynchronizer.get_multiplayer_authority() matches the client’s unique id.
  • In your setup, I suspect that it’s the server who has authority over the MultiplayerSynchronizer node. If that’s the case, $MultiplayerSynchronizer.get_multiplayer_authority() is always 1.
  • So, the if statement is true only when the client’s id is also 1. But only the server can have 1 as their id. Therefore, the function only subtracts health when called by the server.

Solution? There are many ways to structure your code. Here’s how I would do it.
Whenever someone gets shot, always just tell the server, and let the server do all the work.
Instead of calling shot2hit on the client that got hit, just always call it on the server.
r.get_collider().shot2hit.rpc_id(1)
Now whenever someone gets shot, the server calls shot2hit on the collider that got hit. Then the server subtracts health from the player that got shot, and then the MultiplayerSynchronizer tells everyone about it.
And now that the server always calls those functions, you can remove the $MultiplayerSynchronizer.get_multiplayer_authority() == multiplayer.get_unique_id() checks entirely, because you know for a fact that yes it is called on the server.