Buy item script not working

I tried to create this Buy script but it’s not working what could be the problem?
Script:

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Click:Connect(function()
	if player.leaderstats.Coins.Value >= 20 then
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - 20
		
		game.ReplicatedStorage.Tools.Item1:Clone().Parent = player:WaitForChild("Backpack")
	end
end)

Tools folder is in the replicated storage as you can see:
image
Item 1 is inside the Tools folder

And one more thing no errors

Is the script client sided? Let me know first.

Uhm do you mean if it’s a localscript?

Yes. Server sided means you run the code by script. Client means local script.

Uhm could you please specify I am not able to understand i’m sorry

Just tell me if you wrote this on local script.

local player = game.Players.LocalPlayer
local storage = game:GetService("ReplicatedStorage")

script.Parent.MouseButton1Click:Connect(function()
	if player.leaderstats.Coins.Value >= 20 then
		player.leaderstats.Coins.Value -= 20
		local toolClone = storage:WaitForChild("Tools"):WaitForChild("Item1"):Clone()
		toolClone.Parent = player:WaitForChild("Backpack")
	end
end)

If you want the stats to replicate to the server then you’ll either need to make this a server script or have a “RemoteEvent” instance fire the server.

Yep the localscript is called BuyItem

Ok let me try this I am pretty sure it’s gonna work,because you helped me resolve other problems too

Here’s the problem.

You cannot give item by local script. The only way is to use script.

Which means you have to use server side by other ways such as using a remote.

Can I see where you have placed the script?

I suggest doing the tool parenting and money checking in a server script to prevent exploiters. A localscript would look like this;

script.Parent.MouseButton1Click:Connect(function()
      REMOTE_EVENT_PATH:FireServer(tool_name)
end

A server script

REMOTE_EVENT_PATH.OnServerEvent:Connect(function(player, toolName)
       if player.leaderstats.Coins.Value >= 20 then
          player.leaderstats.Coins.Value -= 20
            local toolClone = storage:WaitForChild("Tools"):WaitForChild(itemName):Clone() 		
toolClone.Parent = player:WaitForChild("Backpack")
       end
end)

The remote event path would look like this game.ReplicatedStorage.REMOTEVENTNAME
You can name the remote event anything you want.

Ok sure let me show you:
It is inside a text button called Buy1
image

--LOCAL SCRIPT--
local player = game.Players.LocalPlayer
local storage = game:GetService("ReplicatedStorage")
local re = storage:WaitForChild("BuyRemote")

script.Parent.MouseButton1Click:Connect(function()
	if player.leaderstats.Coins.Value >= 20 then
		re:FireServer()	
	end
end)
--SERVER SCRIPT--
local storage = game:GetService("ReplicatedStorage")
local re = storage:WaitForChild("BuyRemote")

re.OnServerEvent:Connect(function(player)
	if player.leaderstats.Coins.Value >= 20 then
		player.leaderstats.Coins.Value -= 20
		local toolClone = storage:WaitForChild("Tools"):WaitForChild("Item1"):Clone()
		toolClone.Parent = player:WaitForChild("Backpack")
	end
end)

Ok you want me to insert A RemoteEvent inside where exactly?

ReplicatedStorage would be good

Add it to the ReplicatedStorage folder & name it “BuyRemote”.

Sure what could I name it exactly?

ReplicatedStorage is the best place where remotes stay

You can name it BuyItem or something like that