Instance:SetAttribute() won't work

Hi, I’m trying to change an attribute of a sword tool, and I’m trying to change it to the Effect Attribute of it’s player’s HumanoidRootPart. Here is my code:

-- local script
local event = game:GetService("ReplicatedStorage").AddEnchantments
local players = game:GetService("Players")
local plr = players.LocalPlayer

event.OnClientEvent:Connect(function(_, str, spd, uses)
	print("Event fired!")
	local char = plr.Character
	local root = char:FindFirstChild("HumanoidRootPart")
	local effect = root:GetAttribute("Effect")
	local sword = char:FindFirstChild("Sword")
	print(effect)
	if effect == "Hard-Hitting" then
		sword:SetAttribute("Effect", effect)
		root:SetAttribute("Effect", nil)
	elseif effect == "Sturdy" then
		sword:SetAttribute("Effect", effect)
		root:SetAttribute("Effect", nil)
	elseif effect == "Speedy" then
		sword:SetAttribute("Effect", effect)
		root:SetAttribute("Effect", nil)
	end
end)



As you can see, the output displays an error saying that I can’t set an attribute for the Sword, even though it has attributes. I don’t know why this happens. I tried many different ways with a normal Script, but then it didn’t work at all. Any ideas why this is happening?

1 Like

Your sword variable is nil, which means that char:FindFirstChild("Sword") isn’t finding anything.

Is it possible that this code ran before the sword was added to the character? Perhaps it was unequipped and still in the player’s backpack?

1 Like

The button gives the sword to the player and fires the event, too, but I’ll add a wait() and see if it works.

I didn’t get any errors, but the Uses attribute that I wanted to change is still the same.

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