Help with buy button

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

  1. What do you want to achieve? I want my buy button to work

  2. What is the issue? Even tho i have enough currency, the tool is not getting bought

  3. What solutions have you tried so far? I have learnt more about remote functions, yet it doesn’t work. I also looked up multiple videos

--Server Side
local remotecon = {}

local items = game.ReplicatedStorage.Shared:WaitForChild("Tools")
function remotecon.buy(player,toolName)
	local tool = items:FindFirstChild(toolName)
	if tool ~= nil then
		if not player.Ownedtools:FindFirstChild(toolName) and player.Equipped.Value ~= toolName then
			price = tool.Cost.Value
			--plr dosent have tool
			if player.leaderstats.Coins.Value >= price  then
				
				local newtoolval = Instance.new("StringValue")
				newtoolval.Name = toolName
				newtoolval.Parent = player
				player.Current = toolName
				
				for i,v in pairs(player.Backpack:GetChildren()) do
					if v:IsA("Tool") then
						v:Destroy()
					end
				end
				
				local gear = tool:Clone()
				gear.Parent = player.Backpack
				
				player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - price
				
				return "Yes"
			else 
				return "no"
			end
		elseif player.Ownedtools:FindFirstChild(toolName) and player.Equipped.Value ~= toolName then
			for i,v in pairs(player.Backpack:GetChildren()) do
				if v:IsA("Tool") then
					v:Destroy()
				end
			end

			local gear = tool:Clone()
			gear.Parent = player.Backpack
			
			return "Equipped"
		elseif player.Ownedtools:FindFirstChild(toolName) and player.Equipped.Value == toolName then
			for i,v in pairs(player.Backpack:GetChildren()) do
				if v:IsA("Tool") then
					v:Destroy()
				end
			end

			local gear = tool:Clone()
			gear.Parent = player.Backpack
			return "Equipped"
		end
		
	else
		return "error"
	end
end

game.ReplicatedStorage.Remotes.buytool.OnServerInvoke = function(player,toolName)
	remotecon.buy(player,toolName)
end
return remotecon
--Client side

local shop = script.Parent.tools_gui.tools_frame



for i, item in ipairs(Tools) do
	if item:IsA("Tool") then
		local price = item:FindFirstChild("Cost")
		local img = item:FindFirstChild("ImgID")
		local order = item:FindFirstChild("order")
		
		if price and img and order then
			print(item.Name)
			local temp = temp:Clone()
			temp.Parent = scrollingFrame
			temp.Name = item.Name
			temp.img.Image = img.Value
			local priceval = Instance.new("StringValue")
			priceval.Value = price.Value
			priceval.Parent = temp
			temp.Visible = true
			temp.MouseButton1Click:Connect(function()
				shop.tool_img.Image = img.Value
				selectedtemp = temp
				shop.price.Text = price.Value
				shop.name.Text = item.Name
			end)
		else
			continue
		end
	end
end

if selectedtemp ~= nil then
	
end

shop.canbuy.MouseButton1Click:Connect(function()
	if selectedtemp ~= nil then
		local result = game.ReplicatedStorage.Remotes.buytool:InvokeServer(selectedtemp.Name)
	end
end)

Also it doesn’t show me any errors. Help is appreciated.Thank you!

1 Like

First. Check if you’re giving coins to the player locally. If that’s the case, do the money checking on the client side.

1 Like
--Server Side
local remotecon = {}

local items = game.ReplicatedStorage.Shared:WaitForChild("Tools")
function remotecon.buy(player,toolName)
	local tool = items:FindFirstChild(toolName)
	if tool ~= nil then
		if not player.Ownedtools:FindFirstChild(toolName) and player.Equipped.Value ~= toolName then
			price = tool.Cost.Value
			--plr dosent have tool
			if player.leaderstats.Coins.Value >= price  then
				
				local newtoolval = Instance.new("StringValue")
				newtoolval.Name = toolName
				newtoolval.Parent = player
				player.Current = toolName
				
				for i,v in pairs(player.Backpack:GetChildren()) do
					if v:IsA("Tool") then
						v:Destroy()
					end
				end
				
				local gear = tool:Clone()
				gear.Parent = player.Backpack
				
				player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - price
				
				return "Yes"
			else 
				return "no"
			end
		if player.Ownedtools:FindFirstChild(toolName) and player.Equipped.Value ~= toolName then
			for i,v in pairs(player.Backpack:GetChildren()) do
				if v:IsA("Tool") then
					v:Destroy()
				end
			end

			local gear = tool:Clone()
			gear.Parent = player.Backpack
			
			return "Equipped"
		elseif player.Ownedtools:FindFirstChild(toolName) and player.Equipped.Value == toolName then
			for i,v in pairs(player.Backpack:GetChildren()) do
				if v:IsA("Tool") then
					v:Destroy()
				end
			end

			local gear = tool:Clone()
			gear.Parent = player.Backpack
			return "Equipped"
		end
		
	else
		return "error"
	end
end

game.ReplicatedStorage.Remotes.buytool.OnServerInvoke = function(player,toolName)
	remotecon.buy(player,toolName)
end
return remotecon

I don’t think that it will work but you can try.

use remte event to listen for the client

im checking giving coins through the client side only

im not trying to do a one-way communication if so then remote event is the option. But i am trying to do two-way communication so im using remote functions.

it doesn’t work. it showed me errors but i solved your script. My button still isnt working

You should probably add prints to each line to see where it fails.

So everything rpints but the client side aint printing, only in this function:

shop.canbuy.MouseButton1Click:Connect(function()
	if selectedtemp ~= nil then
		local result = game.ReplicatedStorage.Remotes.buytool:InvokeServer(selectedtemp.Name)
		print("buying")
	end
end)

1 Like

No please don’t do that exploiters will just break your game

1 Like

They will break your game. Whatever you do.

1 Like

I changed it now, but still my buy button aint working. Any ideas why?

1 Like

Try spamming print statements everywhere to see where the script breaks

1 Like

thats what, this part isn’t getting print:

shop.canbuy.MouseButton1Click:Connect(function()
	if selectedtemp ~= nil then
		local result = game.ReplicatedStorage.Remotes.buytool:InvokeServer(selectedtemp.Name)
		print("buying")
	end
end)
1 Like

i still haven’t figured out. Please help. Thank you.

1 Like

For reference, i used this video: [FINISHING THE SHOP] How to make a SIMULATOR GAME in ROBLOX STUDIO PART 7! - YouTube

1 Like

Can you do print(result) and see if it says nil?

1 Like

Try doing shop["tool_img"].Image

Also in this script you gotta spam print statements too. What I’d do is literally do a print() at every line. Could be very tedious but it solved 99% of my problems

1 Like

it isn’t printing ANYTHING. if u want here is my output tab:

And i did make it print.

Also for reference i watched this video tutorial: [FINISHING THE SHOP] How to make a SIMULATOR GAME in ROBLOX STUDIO PART 7! - YouTube

1 Like