Why do duplicated nodes share the same variables?

Godot Version

Godot 4.6.3

Question

I’ve been making a game where the player can avoid or attack enemies, although to attack said enemies, you need to pick up weapons available. Though that’s where the problem starts. If I put the a duplicated weapon in the same scene that the original weapon is in, the sprite for the original weapon will use the duplicated weapon’s sprite. The way I programmed it is the sprite will change depending the level of the sword that I apply.

extends Area2D

signal weapon_obtained

@onready var sprite = $Sprite2D

var sword_sprite = { # ATLASTEXTURE REGION
	1 : Rect2(0, 0, 16, 16),
	2 : Rect2(0, 32, 16, 16),
	3 : Rect2(0, 16, 16, 16),
	4 : Rect2(16, 32, 16, 16),
	5 : Rect2(16, 0, 16, 16)
}

@export var sword_level := 1

func _ready() -> void:
	if sword_level > sword_sprite.size():
		sword_level = sword_sprite.size()
	elif sword_level < 1:
		sword_level = 1
	
	sprite.texture.region = sword_sprite[sword_level]

func _on_body_entered(body: Node2D) -> void:
	if body.name == "player":
		queue_free()
		weapon_obtained.emit(sword_level)

Even though it uses the same sprite, the node that receives the “weapon_obtained” signal can differentiate the sword level between the 2 nodes, but the sprite can’t. How do I separate the variable values for both nodes without making a new scene?

You might need to set your texture as local_to_scene. Otherwise all properties of the texture will be copied to every shared instance of that texture.