Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 4 years ago

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

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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)
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. Rinpix 639 — 4y
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. Rinpix 639 — 4y
0
Oh, I'll fix that. youtubemasterWOW 2741 — 4y
Ad
Log in to vote
-1
Answered by 4 years ago

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)
0
but its a string value Oscargamingvr 9 — 4y
0
That is how my shop script works FluffySheep46209 369 — 4y
0
It would error saying "attempt to compare string with number" or something like that. youtubemasterWOW 2741 — 4y

Answer this question