Help with Attempt to Compare nil with Number

I want it so if someone buys it, it makes sure they have enough cash, if they do they get the tool and lose the cash.

Script:

lua
game.ReplicatedStorage.Buy.OnServerEvent:Connect(function(player, price, sword)
	if player.leaderstats.Coins.Value >= tonumber(price) then
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - tonumber(price)
		sword.Parent = player.Backpack
	end
end)

Currently, it prints “error, attempt to compare nil and number”

2 Likes

DO not send data like that, it’s really dangerous and could be easily abused by exploiters. Have your data about items on server and check it on server as well

ex:

--server
Items = {
 Sword = {Price = 666, Inst = ServerStorage.Sword}
}

Event.OnServerEvent:Connect(function(Player,Item)
     local WantedItem = Items[Item] 
     assert(not WantedItem,"Player must be exploiter or error in code")
     if WantedItem then
           local leaderstats = Player:FindFirstChild("leaderstats")
           local coins = leaderstats:FindFirstChild("Coins")
           if coins.Value >= WantedItem.Price then
                  local Sword = WantedItem.Inst:Clone()
                  Sword.Parent = Player.Backpack 
           end
     end
end)

--client

local BuyButton = script.Parent
local ShopEvent

BuyButton.MouseButton1Down:Connect(function()
      ShopEvent:FireServer("Sword")
end)

2 Likes

What is price? It most likely isn’t convertible as tonumber will return nil if the input could not be converted.

price is “100”

local price = 100

so instead of setting it in the script have it set using an int value?

Could you send what is firing the remote event?

Just edited my post, read it.
w

script.Parent.MouseButton1Click:Connect(function()
	local player = game.Players.LocalPlayer
	local price = 100
	local sword = game.ReplicatedStorage.Swords.BetterSword:Clone()
	game.ReplicatedStorage.Buy:FireServer(player, price, sword)
end)

also, when you fire server, do not send Player argument. Because when you :FireServer() the first argument .OnServerEvent will be Player instance that fired the event

thats might be the problem, and as i said before read my edited comment, it would help.

1 Like

It seems rather PlayerObject.leaderstats.Coins.Value is nil. Did you make a typo somewhere? Is Coins any type of value? (IntValue, NumberValue etc.)

I set it up like you said, but now nothing happens.

And you changed variables and things?

Yeah. lemme re check first though

Now it just prints “player must be exploiter or error in code”

server

local Rep = game.ReplicatedStorage.Swords
Items = {
	Sword = {Price = 100, Inst = Rep.BetterSword}
}

game.ReplicatedStorage.BuyBetter.OnServerEvent:Connect(function(Player,Item)
	local WantedItem = Items[Item] 
	assert(not WantedItem,"Player must be exploiter or error in code")
	if WantedItem then
		local leaderstats = Player:FindFirstChild("leaderstats")
		local coins = leaderstats:FindFirstChild("Coins")
		if coins.Value >= WantedItem.Price then
			local Sword = WantedItem.Inst:Clone()
			Sword.Parent = Player.Backpack 
		end
	end
end)

Local:

local BuyButton = script.Parent
local ShopEvent = game.ReplicatedStorage.BuyBetter

BuyButton.MouseButton1Down:Connect(function()
	ShopEvent:FireServer("Sword")
end)

ughhh, try deleting assert uhm

Deleting assert isn’t necessary, assert fires whenever it receives a falsy value. Just remove the not from it.
Also the if statement is redundant because of the assert so it can be removed.

yeah you right, i just said the easiest way

Thank you both very much @Kabutey and @xa_xff it works now. You guys were really helpful

2 Likes

yeah but i forgot to add
coins.Value -= WantedItem.Price

so you can waste your money

:smiley:

1 Like