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

So im trying to make a buy shop gui and i get an error Attempt to Call a userdata value?

Attempt to call a userdata value, i dont know what this means, im on a script, not a local script, here is my code

script.Parent.MouseButton1Click:Connect(function()
	local toolname = script.Parent.Parent.Itemname.Text
	local cost = script.Parent.Parent.Price.TextLabel.Text
	local tool = game.ReplicatedStorage(toolname)
	local money = script.Parent.Parent.Parent.Parent.Parent.Parent
	if money <= tonumber(cost) then
		money = money - cost
		local tool = tool:Clone()
		tool.Parent = game.Players.LocalPlayer.Backpack
	end
			
end)

2 answers

Hello.


Instead of using (toolname), you must use [toolname], as in Roblox Lua, that's how you index instances despite the dot operator.


Also, game.Players.LocalPlayer won't work in a normal script, so you have to use a RemoteEvent, or get the player by using script.Parent.Parent.Parent.Parent.


I assume you wanted to be able to buy the item if your money is greater or equal to the cost, not equal to or less than the cost (which is what you did). I've also fixed that.


Fixed Script (without the RemoteEvent):

script.Parent.MouseButton1Click:Connect(function()
    local toolname = script.Parent.Parent.Itemname.Text
    local cost = script.Parent.Parent.Price.TextLabel.Text
    local tool = game.ReplicatedStorage[toolname]
    local money = script.Parent.Parent.Parent.Parent.Parent.Parent
    if money >= tonumber(cost) then
        money = money - cost
        local tool = tool:Clone()
        tool.Parent = game.Players.LocalPlayer.Backpack
    end

end)

3 comments

0

Wait a second, wouldn't we only deduct from the money variable if the amount of money we have is greater than or equal to the cost? If it's less than the cost, then we shouldn't be able to purchase the item.

0

Also, if money is an instance(given how he references it with the variable it probably is), then wouldn't we deduct from its value property and not the entire instance? Seems kind of odd to me. I know this is an answer and not written by the guy who asked the question, but yeah.

0

Oh, I'll fix that.

Urm I don't thin you needed tonumber so try this

script.Parent.MouseButton1Click:Connect(function()
    local toolname = script.Parent.Parent.Itemname.Text
    local cost = script.Parent.Parent.Price.TextLabel.Text
    local tool = game.ReplicatedStorage(toolname)
    local money = script.Parent.Parent.Parent.Parent.Parent.Parent
    if money <= cost then
        money = money - cost
        local tool = tool:Clone()
        tool.Parent = game.Players.LocalPlayer.Backpack
    end

end)

3 comments

0

but its a string value

0

That is how my shop script works

0

It would error saying "attempt to compare string with number" or something like that.

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