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

Why is this script for Shop Button not Working?

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

#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!

7 comments

0

Number 2 is most important. But so is 1 :P

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?

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

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.

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!

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...

0

You're welcome

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