Unable to make accessory reappear in tool

Hello there! I am trying to make a tool that makes an accessory disappear after the tool is equipped, and reappear after the tool is unequipped. The disappearing works fine, but the reappearing isn’t really working. This is the code I have written:

local tool = script.Parent

local character = game.Players.LocalPlayer.Character

tool.Equipped:Connect(function()

print("The pillow was equipped.")

tool.Parent.DefaultPillowAccessory.Handle.Transparency = 1 --The tool's parent is the character, where the accessory is, and I can access it from there.

end)

tool.Unequipped:Connect(function()

print("The pillow was unequipped.")

character.DefaultPillowAccessory.Handle.Transparency = 0 --Grabbing the accessory from the character since the tool is now in Backpack and not in the character

end)

Unfortunately, I get an error while trying to make the accessory reappear:

18:31:45.593 Players.SpeakerDev.Backpack.DefaultPillow.LocalScript:11: attempt to index nil with 'DefaultPillowAccessory' - Client - LocalScript:11

Can someone explain why this doesn’t work? Possibly give a suggestion on what to do? (I’m a bit new to scripting, so explaining your suggestion will help a lot. All feedback is appreciated anyway!)

I think the reason why this happened is because when the tool is unequipped, the script cant find the pillow from the player since it’s gone so u need a better way to do store the tool. and also, in your script, are you sure you want the pillow handle transparency to be 1 when equipped?

I’m not deleting the accessory when I equip the tool, I make the handle inside the accessory transparent so you can’t see it anymore. The accessory and its handle should still be accessible, but it’s giving an error. And yes, I do want to make the pillow handle’s transparency to 1 when it’s equipped. The only other option from that is to destroy the accessory, but I want the accessory to reappear after the tool is unequipped.

ohh, actually the problem in your script is when u did character.DefaultPillowAccessory.Handle.Transparency = 0

how is the character supposed to be able to connect to the model???

I’m not sure what you’re saying here. Could you expand a bit more on this, like why the character can’t connect to it? (Also, if you want more info so you can understand better, the script is a LocalScript inside a tool that will now be located in the Backpack after unequipping).

what i mean is that the items in workspace is not a parent of the character, but the workspace is the parent of the character. so you’ll have to use

workspace.DefaultPillowAccessory.Handle.Transparency = 0

or character.Parent.DefaultPillowAccessory.Handle.Transparency = 0

in line 17

I now get this error:
19:06:10.311 Players.SpeakerDev.Backpack.DefaultPillow.LocalScript:11: attempt to index nil with 'Parent' - Client - LocalScript:11

Also, I thought accessories are always a child of the character? Otherwise, they wouldn’t exist on the character at all.

then use workspace.DefaultPillowAccessory.Handle.Transparency = 0

and if this doesnt work, then send a file of a game that as the same stuff you’re trying to do so i can know the actual names i can put in the script.

As the error message says, your character isn’t defined.
The most probable reason is that since you are defining your character inside a script that’s under your tool, the script is going to run before the character is even loaded. (You can test this by immediately printing the character after the line where you declare it. print(character). If it outputs nil, then it’s the case.

What you can do to fix this is:
1/ Stop placing your scripts under tools, this is only creating confusion. You should rather place your local scripts in either one of these two folders image
2/ Place your script is StarterCharacterScripts, since it has to run when and only when your character is added. (If it’s not then why even run the code? You can’t give him a tool, make his tool disappear or anything + even if you waited long enough for the character to be loaded it’d break at respawn)
3/ Change the references to your tool variable, and change the way you give it to your character.
You can, for example, make a clone of the tool

local tool = game.ReplicatedStorage.Where.Your.Tool.Is:Clone() -- :Clone() creates a clone of your tool as the name implies.
tool.Parent = game.Players.LocalPlayer.Backpack -- Add the cloned tool to the player's backpack, making it appear in your toolbar

The rest of the logic should be fine.

2 Likes

or try @itsmeantwanforreal1 's method