Leaderstats Issue

Ive been trying to do a gui shop to buy items. When a button is pressed, a remote event gets triggered and then a script in serverscript service checks if you have enough currency to buy it. But im trying to make that you need materials to buy it. If you can help, i would appreciate it.

BuyToolEvent.OnServerEvent:Connect(function(player,ToolName, ItemPrice, CurrencyType)
	local Tool = ServerStorage:FindFirstChild("CraftingTools"):FindFirstChild(ToolName)
	local CurrencyToUse = Iron
	if Tool then
		if player.leaderstats."CurrencyType".Value >= ItemPrice then
			-- player.leaderstats.Iron.Value >= 6
			
			player.leaderstats.Iron.Value = player.leaderstats.Iron.Value -6
			game.ServerStorage.CraftingTools.IronBat:Clone().Parent = player.Backpack
		end
		
	
	end
	
end)
4 Likes
BuyToolEvent.OnServerEvent:Connect(function(player, ToolName, ItemPrice, CurrencyType)
	local Tool = game.ServerStorage:FindFirstChild("CraftingTools"):FindFirstChild(ToolName)
	if Tool then
		if player.leaderstats[CurrencyType].Value >= ItemPrice then
			player.leaderstats[CurrencyType].Value -= 6
			game.ServerStorage.CraftingTools.IronBat:Clone().Parent = player.Backpack
		end
	end
end)
1 Like

Im gettinjg this error: 09:45:04.839 ServerScriptService.BuyTools:24: attempt to compare Instance <= number - Server - BuyTools:24

What are you sending in the event?

the event is in replicated storage. The event get fired in starter gui (LocalScript) to a script in server script service

But what’s being sent through?
Show me the part where the event is fired.

YesButton.MouseButton1Click:Connect(function()
local ToolName = script.Parent.Parent.IronBat.Name
BuyToolEvent:FireServer(ToolName, ItemPrice, CurrencyType)
end)

What is CurrencyType? Is it an Instance or a value?

it’s definitely a string. i know that for sure

local CurrencyType = “Iron”

There is a leaderstat called iron

what’s ‘ItemPrice’?

it says it’s an instance according to the error message

I have my doubts because of the error they got:

It says <= rather than >= like the script, so it’s clearly reversed

BuyToolEvent.OnServerEvent:Connect(function(player,ToolName, ItemPrice, CurrencyType)
local Tool = ServerStorage:FindFirstChild(“CraftingTools”):FindFirstChild(ToolName)
if Tool then
if player.leaderstats[CurrencyType].Value >= ItemPrice then
player.leaderstats[CurrencyType].Value = player.leaderstats[CurrencyType].Value -ItemPrice
Tool:Clone().Parent = player.Backpack
end
end
end)

Still the error shows up if you try to compare an Instance to a number

@dav2777 In the LocalScript where you have the BuyToolEvent:FireServer, is the CurrencyType a string value there as well or is it a IntValue/NumberValue named “Iron”?

the script has a lot of errors, the one i sent fixes them.
My question is, what is ‘ItemPrice’

run this:

BuyToolEvent.OnServerEvent:Connect(function(player, ToolName, ItemPrice, CurrencyType)
    print(ItemPrice, typeof(ItemPrice))
	local Tool = game.ServerStorage:FindFirstChild("CraftingTools"):FindFirstChild(ToolName)
	if Tool then
		if player.leaderstats[CurrencyType].Value >= ItemPrice then
			player.leaderstats[CurrencyType].Value -= ItemPrice
			game.ServerStorage.CraftingTools.IronBat:Clone().Parent = player.Backpack
		end
	end
end)

ItemPrice is the price of the item
and currencyType is for example: Iron, diamonds, gold, rock, wood

I know what itemprice is intended to be, but according to the error message, it’s an instance and not a number

so what does the error this mean?