Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Shop script error saying attempt to compare nil and user value??

So i am trying to make a shop script and when the player purchases something it fires to the server, here is the code for that

script.Parent.MouseButton1Click:Connect(function()
    local toolname = script.Parent.Parent.Itemname.Text
    local cost = script.Parent.Parent.Price.TextLabel.Text
    local tool = game.ReplicatedStorage.Tools[toolname]
    local money = script.Parent.Parent.Parent.Parent.Parent.Parent
game.ReplicatedStorage.Purchase:FireServer(money, toolname, cost, tool)

end)

then the server makes sure that the player has enough money to purchase the item, but when i test it i get an error saying attempt to compare userdata value and nil here is that script

game.ReplicatedStorage.Purchase.OnServerEvent:Connect(function(money, tool, cost)
	local money = money
    if money >= tonumber(cost) then
        money = money - cost
        local tool = tool:Clone()
        tool.Parent = game.Players.LocalPlayer.Backpackk
    end


end)

2 answers

Hello.


Problem:

  • There's an automatic parameter in the OnServerEvent event called "player". It is the player who fired that RemoteEvent.

  • You misspelled "Backpack"

  • You used LocalPlayer in a ServerScript which wouldn't work because ServerScripts don't run on clients.

  • You fired the tool's name instead of the tool itself.

  • You kinda mixed up the order of the parameters/arguments.


Solution:

  • Add that parameter in the OnServerEvent event. Also, if you didn't use the player parameter, you'd be comparing the player "money" with the cost of the item.

  • Spell "Backpack" right.

  • Use the player parameter instead of LocalPlayer.

  • Fire the tool through the RemoteEvent, not the tool's name.

  • Fix the order of the parameters/arguments.


Recommendations:

  • You don't need the local money = money variable as it's a parameter, which pretty much makes it a variable itself.

Fixed ServerScript:

game.ReplicatedStorage.Purchase.OnServerEvent:Connect(function(player, money, tool, cost)
 if money >= tonumber(cost) then
        money = money - tonumber(cost)
        local tool = tool:Clone()
        tool.Parent = player.Backpack
    end


end)

Fixed LocalScript:

script.Parent.MouseButton1Click:Connect(function()
    local toolname = script.Parent.Parent.Itemname.Text
    local cost = script.Parent.Parent.Price.TextLabel.Text
    local tool = game.ReplicatedStorage.Tools[toolname]
    local money = script.Parent.Parent.Parent.Parent.Parent.Parent
game.ReplicatedStorage.Purchase:FireServer(money, tool, cost)

end)

6 comments

0

it says for line 5, attempt to call a nil value

0

That's not supposed to happen..

0

Which script is it?

0

2nd one

0

LocalScript or ServerScript?

0

Server

Hello! You need to fix line 5.

local money = script.Parent.Parent.Parent.Parent.Parent.Parent.Value

OR

local money = script.Parent.Parent.Parent.Parent.Parent.Parent.Text

And also, you spelled backpack wrong:

tool.Parent = game.Players.LocalPlayer.Backpack --Correct now

ALSO

--fix the first line of the second script to this

game.ReplicatedStorage.Purchase.OnServerEvent:Connect(function(player, money, tool, cost)

You ALWAYS have to have an argument called player when the client is communicating with the server. Hope it helps!

6 comments

0

Im an idiot, thank you so much

0

But it says attempt to copare user data value and nil on line 3 second script

0

i dont think you have to use tonumber, just put cost

0

Yeah

0

Ohh, I found the error, this should do it,

0

Look at it now

This discussion is preserved as it was. New answers, comments, and votes are closed.