Why this code don't work, im clicking upgrade button but it doing nothing

im very started and trying to make a game maybe im too stupid but there also nothing in the output pls help

local Player = game.Players.LocalPlayer
local Money = Player.leaderstats.Money
local Multi = Player.Multi

local ReplicatedStorage = game.ReplicatedStorage
local Remotes = ReplicatedStorage.Remotes
local Remote = Remotes.DoubleMoneyGain

local screenGUI = script.Parent
local StatsHolder = screenGUI.GuiHolder.StatsHolder
local UpgradesHolder = screenGUI.GuiHolder.UpgradesHolder

local DoubleMoneyGainButton = UpgradesHolder.DoubleMoney.UpgradeButton

local function DoubleMoneyGain()
	local NumberOfUpgrades = DoubleMoneyGainButton.NumberOfUpgrades
	if NumberOfUpgrades == 0 then
		DoubleMoneyGainButton.MouseButton1Click:Connect(function()
			Money.Value -= 5
			Multi.Value *= 2
			NumberOfUpgrades.Value += 1
			Remote:FireServer()
		end)
	end
end
4 Likes

What exactly is the problem here?

2 Likes

Well, you never called the function. It wouldn’t work.

2 Likes

I need to type

DoubleMoneyGain()

in the end of a script right? or what explain pls

2 Likes

i click upgrade button and nothing happen

1 Like

Like @UdayS said, you never called the function.

1 Like

just explain what i need to do in code PLS

Call the function like this: DoubleMoneyGain().

i do it still nothing
image

Hello! This is what you need to do. Let me know if you have any questions.

local button = -- path to your button that you want the player to click

button.MouseButton1Click:Connect(function()
DoubleMoneyGain()
end)

Hope this helps! :slight_smile:

like this? still nothing
image

Hmm. That’s weird.

I see that you have a RemoteEvent that you’re firing to the server. Can I see the client script of this system and the hierarchy of the GUI please?

1 Like

oh wait i’ll delete RemoteEvent Connecting

image

1 Like

Something that might help is using the WaitForChild() method on the GUI variables. The children of the ScreenGUI may not be loaded in yet when you play-test the game, so when the button is clicked, your function won’t fire. You could also try adding a task.wait(5) at the top of your script.

2 Likes

Can we see your output? I agree with @Kittylitterking123 that it’s probably a loading timing issue

1 Like
local Player = game.Players.LocalPlayer
local Money = Player.leaderstats.Money
local Multi = Player.Multi

local screenGUI = script:WaitForChild("ScreenGui")
local GuiHolder = screenGUI:WaitForChild("GuiHolder")
local UpgradesHolder = GuiHolder.UpgraderesHolder
local StatsHolder = GuiHolder.StatsHolder

local DoubleMoneyGainButton = UpgradesHolder.DoubleMoney.UpgradeButton


local function DoubleMoneyGain()
	local NumberOfUpgrades = DoubleMoneyGainButton.NumberOfUpgrades
	if NumberOfUpgrades == 0 then
		Money.Value -= 5
		Multi.Value *= 2
		NumberOfUpgrades.Value += 1
	end
end

DoubleMoneyGainButton.MouseButton1Click:Connect(function()
	DoubleMoneyGain()
end)


@FloofyNezzled

Why are you trying to access the ScreenGUI like it’s a child of the script? By looking at the hierarchy, this will error. It should be equal to script.Parent.

2 Likes

image
?

Could you try this ?
Also, changing the values in client side will make it “invisible” in server side, so nothing will be saved and nothing will apply to your real stats.

local PlayerService = game:GetService("Players")

local Player = PlayerService.LocalPlayer

local Multi = Player:WaitForChild("Multi", 300)
local Leaderstats = Player:WaitForChild("leaderstats", 300)
local Money = Leaderstats and Leaderstats:WaitForChild("Money", 30)

local screenGUI = script.Parent
local GuiHolder = screenGUI:WaitForChild("GuiHolder", 30)
local UpgradesHolder = GuiHolder and GuiHolder:WaitForChild("UpgraderesHolder", 30)
local StatsHolder = GuiHolder and GuiHolder:WaitForChild("StatsHolder", 30)

local DoubleMoneyGainButton = UpgradesHolder:FindFirstChild("UpgradeButton", true)
local NumberOfUpgrades = DoubleMoneyGainButton:FindFirstChild("NumberOfUpgrades")

local function DoubleMoneyGain()
	if NumberOfUpgrades.Value == 0 then
		Money.Value -= 5
		Multi.Value *= 2
		NumberOfUpgrades.Value += 1
	end
end

DoubleMoneyGainButton.MouseButton1Click:Connect(DoubleMoneyGain)