You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
buying items from a shop.
What is the issue? Include screenshots / videos if possible!
button click not activating code
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.
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.
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
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))
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
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)