Need help wtih basic shop

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    buying items from a shop.
  2. What is the issue? Include screenshots / videos if possible!
    button click not activating code
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    im literally spending hours at a time looking at this forum trying to fix simple things and im fed up especially when it was working and now doesnt for no clear reason so this is for my sanity
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local player = game:GetService("Players").LocalPlayer
local money = player.leaderstats.Cash.Value
local price = 1500 -- Chane "100" to the price of the gear.
local tool = game.ServerStorage.Allitems.AR3 
local buybutton = script.Parent

local function buy()
	if money >= price then
		money = money - price
		tool:Clone().Parent = player.Backpack
		tool:Clone().Parent = player.PlayerGui

	end
end
buybutton.MouseButton1Down:Connect(buy)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

2 Likes

Could we get a bit more info here?

Is this a Localscript? You cannot access Serverstorage or ServerScriptService on a Localscript. Item giving should be done on the server instead through a RemoteEvent.

Errors if any?

2 Likes

This has to be in a server script so you can’t use LocalPlayer or you can fire an event for the tool cloning part

2 Likes

it is a local script and no errors tried using print but nothing shows when i try
so should i use a sever script

yes use a server script or if you want, fire an event to do the things the localscript can’t

2 Likes

You should move the tool giving part to a serverscript and use a RemoteEvent to send info between server and client.

Essentially, the UI part should be done on client, the giving and checking money part should be done on Server.
If you haven’t ever used RemoteEvents, you should go ahead and look them up.

PSA there’s a slipup in second line of your code

2 Likes

server

local Replicatedstorage = game:GetService("ReplicatedStorage")

local remoteEvent = Replicatedstorage:WaitForChild("ShopRemoteEvent")
local guns = game.ServerStorage.Allitems

local function Playerpurchase(player,item,price)
	if player.leaderstats.Cash >= price then
		player.leaderstats.Cash = player.leaderstats.Cash - price
		guns.item:Clone().Parent = player.Backpack
	end
	
end


remoteEvent.OnServerEvent:Connect(Playerpurchase)

client, is this right? nothing happens when i click the button no errors






local Replicatedstorage = game:GetService("ReplicatedStorage")

local remoteEvent = Replicatedstorage:WaitForChild("ShopRemoteEvent")



local player = game:GetService("Players").LocalPlayer
local money = player.leaderstats.Cash.Value
local price = 1500
local tool = "AR3"
local buybutton = script.Parent

buybutton.MouseButton1Click:Connect(remoteEvent:FireServer(player,tool,price))

2 Likes

Client cannot access Serverstorage. Either move it, or call it to the server by string.

2 Likes

i edited it and same thing is happening

1 Like

Edit this the localscript to this:

local event = game.ReplicatedStorage:WaitForChild("ShopRemoteEvent")
script.Parent.MouseButton1Down:Connect(function()
local tool = "AR3"
local price = 1500
event:FireServer(tool,price)
end)

and the serverscript to this:

local event = game.ReplicatedStorage:WaitForChild("ShopRemoteEvent")
local guns = game.ServerStorage.Allitems
event.OnServerEvent:Connect(function(player,tool,price)
		player:WaitForChild("leaderstats").Cash = player:WaitForChild("leaderstats").Cash - tonumber(price)
		local c = guns.tool:Clone()
        c.Parent = player.Backpack
	end)

if it errors screenshot it and send it as an reply

1 Like

oops, edit it line 4 to

player:WaitForChild("leaderstats").Cash.Value = player:WaitForChild("leaderstats").Cash.Value - tonumber(price)
1 Like

ok

its taking the money but no item

	if tool == "AR3" then
	local c = guns.AR3:Clone()
		c.Parent = player.Backpack
	end

replace lines 6-7 with this or change the “guns.AR3” to whatever its named in the allitems folder

1 Like

money goes negative any fixes?

you have less money than the price, therefore it goes into the negatives. set your leaderstat value to be higher + here is a check to ensure the people who buy items have enough money

local event = game.ReplicatedStorage:WaitForChild("ShopRemoteEvent")
local guns = game.ServerStorage.Allitems
event.OnServerEvent:Connect(function(player,tool,price)
if player:WaitForChild("leaderstats").Cash.Value >= price then
		player:WaitForChild("leaderstats").Cash.Value = player:WaitForChild("leaderstats").Cash.Value - tonumber(price)
	if tool == "AR3" then
	local c = guns.AR3:Clone()
		c.Parent = player.Backpack
	end
else
warn("You don't have enough money!")
end
	end)
1 Like