Clicking Gui Button does not give tool

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.
image

Here is my workspace too.

The script in ServerScriptService is where the problem is located.

1 Like

LocalPlayer does not exist on the server.

1 Like

Thanks, but I have a new occurring issue of it not showing on a different client.

What I see

Client See’s

1 Like

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.

1 Like

What do you mean clone the tool on the server?

1 Like

In your server script, clone the tool from replicated storage into the player’s backpack.

1 Like

Im pretty sure I’ve done that already, and it works.

1 Like

You got an error with FindFirstChild. Try :WaitForChild

Also if this is in a server script you can’t do LocalPlayer after game.Players

1 Like

You aren’t setting its parent to the backpack in the else block which I’m guessing is the part that fires the client.

1 Like

image
so like this?

1 Like

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.

1 Like


Still doesn’t show on client even after making the parent to the backpack

1 Like

Can you show your local script?

1 Like

Wait, I think I made a mistake there because the script I’ve been showing in the one inside the one inside ServerScriptServices

image
image

1 Like

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)
1 Like

You’re still cloning and setting the parent of toolClone on the client.

1 Like

I changed to backpack and this is on the ServerScriptService one but its still not showing it
image

1 Like

Whenever I change it on the localscript it gives me an error
image

1 Like

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.

1 Like
local player = nil

game:GetService("Players").PlayerAdded:Connect(function(plr)
player = plr
end)

if im correct thats a cheap way to define the local player lol

1 Like