GetAttribute() returning wrong value after player resetting

Right now I’m setting player attributes to their character when it spawns. This includes things like their active skillset. However, when they respawn I’m getting the wrong values when I call GetAttribute().

Here is my script when the player spawns (in ServerScriptService):

function setAttributes(character)
	if character then
		local player = Players:GetPlayerFromCharacter(character)
		local humanoid = character:FindFirstChild("Humanoid")
		if humanoid then
			character:SetAttribute("State", "Idle")
			character:SetAttribute("Stunned", false)
			character:SetAttribute("Iframes", false)
			character:SetAttribute("inCombat", false)
			character:SetAttribute("lastHitTime", 0)
			character:SetAttribute("Skillset", Profiles[player].Data.Skillset)
			print("set the skillset attrribute as "..Profiles[player].Data.Skillset)
			character:SetAttribute("Sub_Skillset", "Awakening")
			character:SetAttribute("FunctionID", nil)
		end
	end
end

And here is my script to activate a move based on the skillset (just the part receiving the attribute tho; in StartPlayerScripts):
local function onCharacterAdded()
	Skillset = Functions.WaitForAttribute(Character, "Skillset") or nil
	print(Skillset)
	print(Character:GetAttribute("Skillset"))
end

The above function WaitForAttribute() is just in a module script that continually waits for the attribute to not be nil and return the value. Just to wait until server actually assigns the attributes. I have the second and normal GetAttribute() because I thought maybe the above one was causing an issue.

I also have a script just continually checking what the attributes are for this debugging purpose.

Here is the console output:

  17:24:30.673 set the skillset attrribute as Itadori  -  Server - Main:36
  17:24:30.712  -------------------CHARACTER SPAWNED-----------------  -  Client - LocalScript:4
  17:24:30.714  Gojo  -  Client - KeyInput:31
  17:24:30.715  Gojo  -  Client - KeyInput:32
  17:24:30.715  {}  -  Client - KeyInput:35
  17:24:30.727   ▼  {
                    ["Iframes"] = false,
                    ["Skillset"] = "Itadori",
                    ["State"] = "Idle",
                    ["Stunned"] = false,
                    ["Sub_Skillset"] = "Awakening",
                    ["inCombat"] = false,
                    ["lastHitTime"] = 0
                 }  -  Client - LocalScript:9

As you can see the localscript is getting “Gojo” while it was actually assigned as Itadori. The skillset was Gojo before the player reset, and now it is Itadori, but the localscript still thinks its Gojo. Also, attributes reset when the player resets because it is on their character model, so I have no idea why it’s still Gojo.

I’m stupid. I had a Character variable at the top which was set when the first player joined, and I continued to use that after respawn. To fix, I just made it so the onCharacterAdded function receives character from Player.CharacterAdded

local function onCharacterAdded(character)
	Skillset = Functions.WaitForAttribute(character, "Skillset") or nil
	print(Skillset)
end

I am guessing you bind onCharacterAdded() like Players.CharacterAdded:Connect(onCharacterAdded)

In this case, you should take the character argument in your onCharacterAdded function. redefine the function as

‘’’
local function onCharacterAdded(character)
Skillset = Functions.WaitForAttribute(character, “Skillset”) or nil
print(Skillset)
print(character:GetAttribute(“Skillset”))
end
‘’’

What you are doing right now is just taking the Character variable you probably defined somewhere up and therefore it is the same character everytime.

1 Like

yeah this was right, thanks. I realized just before you commented haha

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.