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)
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.
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
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…
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.
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?
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.