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

Why is this script for Shop Button not Working?

Asked by 8 years ago

So, I made a LocalScript inside of an ImageButton that takes away from a value inside of a separate GUI and clones an item from Lighting and into a the clicker's backpack, but it doesn't seem to be working. I also cannot found any errors, so I was wondering if anyone could find something that I may have overlooked or screwed up on. Thanks for your time!

local money = game.Players.LocalPlayer.PlayerGui.MoneyGui.Frame.Amount.Money.Value
local price = 5

script.Parent.MouseButton1Click:connect(function()
    if money >= price then
        money = money-5
        local water = game.Lighting.Water:Clone()
        water.Parent = game.Players.LocalPlayer.Backpack
        print(game.Players.LocalPlayer.Name .. "has just bought Water!")
    end
end)

1 answer

Log in to vote
1
Answered by 8 years ago

#1, Try putting water in ReplicatedStorage.

#2, Change how you define money by getting rid of the .Value at the end

Using Lighting to store items is a bad idea and just isn't used anymore. You also can't use .Value when defining Variables. This is just how Roblox Lua works. The resulting code after moving water to ReplicatedStorage and Changing how you define money would look something like this,

local money = game.Players.LocalPlayer.PlayerGui:WaitForChild("MoneyGui"):WaitForChild("Frame"):WaitForChild("Amount"):WaitForChild("Money")  --Remove.Value and add some Waits
local price = 5

script.Parent.MouseButton1Down:connect(function()
    if money.Value >= price then --Use .Value when refurring to the variable's value
        money.Value = money - price
        local water = game.ReplicatedStorage.Water:Clone() --Move water to replicated storage
        water.Parent = game.Players.LocalPlayer.Backpack
        print(game.Players.LocalPlayer.Name .. "has just bought Water!")
    end
end)

That should work. Good Luck!

0
Number 2 is most important. But so is 1 :P User#11440 120 — 8y
0
I greatly appreciate the time you have put into helping me out, but I still can't get this to work. How exactly were you suggesting I add "WaitForChild"s to the 1st line? UngracefulUnicorn 40 — 8y
0
Lol, what's not working now? Did you try the code? This should work from what I can tell. I'll update my answer with some wait for childs User#11440 120 — 8y
0
I have done exactly what you've told me to do, and I am at a bit of a loss. Not sure what else to do but seek more help. Thank you so much for your effort. UngracefulUnicorn 40 — 8y
View all comments (3 more)
0
Yeah cool great. What's wrong with it? Are there no error messages? Does it print? Is the script disabled? But oh well. Good luck! User#11440 120 — 8y
0
Script is enabled, there are no error messages, and it does not print. Anyways, I'm going to rest my brain now. Thanks again... UngracefulUnicorn 40 — 8y
0
You're welcome User#11440 120 — 8y
Ad

Answer this question