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.

LocalPlayer does not exist on the server.

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

What I see

Client See’s

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.

What do you mean clone the tool on the server?

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

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

You got an error with FindFirstChild. Try :WaitForChild

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

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

image
so like this?

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.


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

Can you show your local script?

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

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)

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

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

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

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.

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