I’ve been trying to make a script where if you click a button it’ll give you that tool but unfortunately I am not able to find the solution to this problem.
The following script is
local replicatedStorage = game:GetService("ReplicatedStorage");
local player = game:GetService("Players").LocalPlayer;
local backpack = player:FindFirstChild("Backpack")
local button = script.Parent;
local toolName = "Python Revolver";
local toolClone;
local toolActivated = false;
game.ReplicatedStorage.ToolGiver.OnServerEvent:Connect(function(toolClone)
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"
game.ReplicatedStorage.ToolGiver:FireClient(toolClone)
end
end)
end)
Whenever I load into the game it gives me this error.
You should clone the tool on the server and then on the server parent it to the player’s backpack. You’re only setting the tool’s parent locally from what I see.
Yeah. You’re also handling UI events on the server which I would advise against doing. You should use remote events and fire the server when necessary and handle all UI-related affairs on the client.
This is my local script. Its the same thing but without the fire events and that stuff.
local replicatedStorage = game:GetService("ReplicatedStorage");
local player = game:GetService("Players").LocalPlayer;
local backpack = player:WaitForChild("Backpack");
local button = script.Parent;
local toolName = "Python Revolver";
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)
Can you please show your current code in the Server Script and Local Script?
I see the current issue you are facing right now, but it’s hard to tell what other problems you will face without the updated code.