Help in the script

I ask you to help me in the script, in general, closer to the point, I made a store in which I buy an item, but the problem is that when I buy an item, the script does not work for this item, but when I move this item to the StarterPack, it works.

I ask everyone who can help me with this problem, I will be sincerely grateful.

And here is the script itself.

local player = game.Players.LocalPlayer

script.Parent.MouseButton1Down:Connect(function()
	if player.leaderstats.Coins.Value >= 80 then
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - 80
		
		game.ReplicatedStorage.Gears.GravityCoil:Clone().Parent = player.Backpack
	end
end)
1 Like

Hello @RoStar_Dev! :happy1:
I am a newbie to scripting, but I think I fixed your code, and here it is!

local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)

script.Parent.MouseButton1Down:Connect(function()
	if player and player.leaderstats and player.leaderstats.Coins and player.leaderstats.Coins.Value >= 80 then
		print("Purchase successful!")
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - 80
		
		local gravityCoil = game.ReplicatedStorage.Gears.GravityCoil:Clone()
		if gravityCoil then
			gravityCoil.Parent = player.Backpack
		else
			warn("GravityCoil model not found in ReplicatedStorage.Gears.")
		end
	else
		print("Insufficient coins or invalid player.")
	end
end)

If you found my answer useful, do not forget to mark it as solved :white_check_mark:.
@Katastro5 :cool:

You can try putting this inside of a ServerScript and setting the RunContext to “Client”. Try that and see if it works. Just copy your code and paste it inside of your new script. Standard LocalScripts will always run under certain things. This is not the case for RunContext being switched to “Client”. It can run anywhere but will only run for the client which is the LocalPlayer.