How to make a tool equip?

Hi everyone, I read this post to try and make a GUI to equip an item located in Server Storage, but the game returns an error claiming that the tool isn’t a part of ServerStorage. Do you know why that is and how to fix this? Here is my code:

local tool = game.ServerStorage.x2SpeedItem
local klone = tool:Clone()

script.Parent.MouseButton1Click:Connect(function(plr)
	if klone.Parent ~= plr.Backpack then
		klone.Parent = plr.Backpack
		script.Parent.Text = "Unequip"
	else
		klone.Parent = game.ServerStorage
		script.Parent.Text = "Equip"
	end
end)
2 Likes

The client can’t see the contents of ServerStorage, as it’s exclusive to the server.
You would have to store your tool in ReplicatedStorage or ReplicatedFirst in this case.

2 Likes

Which one do you think is the best to do so?

1 Like

I now have another error: Players.MrDaBigBoss.PlayerGui.ScreenGui.Frame.TextButton.LocalScript:5: attempt to index nil with ‘Backpack’
I think that its claiming my backpack doesn’t exist somehow…

2 Likes

Try ReplicatedFirst, it replicates… first… so anything within it should be there before this script runs.

Let me help you with your code just a little…

local replicatedStorage	= game:GetService("ReplicatedStorage");
local player 			= game:GetService("Players").LocalPlayer;
local backpack 			= player:WaitForChild("Backpack");

local button 			= script.Parent;

local toolName 			= "x2SpeedItem";
local toolClone;
local toolActivated		= false;

button.MouseButton1Click:Connect(function()
	--check if the tool exist
	if not toolClone then
		--if the tool doesn't exist already, give the player the tool
		toolClone = replicatedStorage[toolName]:Clone();
		
		toolClone.Activated:connect(function() toolActivated = true end);
		toolClone.Deactivated:connect(function() toolActivated = false end);
		
		toolClone.Parent = backpack
		
		button.Text = "Unequip"
	else
		--if the tool doesn't exist, then we should remove the tool, effectively setting toolClone to nil
		
		if toolActivated then
			toolClone:Deactivate();	
		end
		
		toolClone.Parent = nil;
		toolClone = nil;
		
		button.Text = "Equip"
	end
end)

Okay, I made an edit to my code, the edit I made is this:
If the player is holding the tool, we need to deactivate it first before we remove it.

Edit:
Also yikes I just realized I used ReplicatedStorage instead of ReplicatedFirst, it should do the same trick though in this case

8 Likes

Wow thank you! Although this works, it only works for 1 cycle. What I mean by that is that I can equip it once and unequip it, but can’t re-equip it after that…

3 Likes

Opps, sorry. I made an edit to my code. Try that out.

I’m also going to code you a little more because I think you’re soon going to run into an error while using the tool.

1 Like

You shouldn’t be using ReplicatedFirst for this kind of thing anyways. ReplicatedFirst is really supposed to be for stuff like a custom loading screen that needs loading priority. Your LocalScripts shouldn’t have to worry about anything not existing in ReplicatedStorage unless you Parent it there at runtime.

3 Likes

I just realised while testing with multiple players that the item doens’t show on a player’s screen. What I mean by that is that if I am holding an item, my friend will not see it. How can I fix that?

1 Like

You’re giving them the tool in a LocalScript, so it won’t replicate to the server. You’d need to use a RemoteEvent to tell the server that you want to be given a tool.

2 Likes

big oof, I don’t really know how to use Remote events…