If statement not running in localscript

im working on a clicker game, and this script should give the player 1 more peguin per click if they have 10 peguins, and then remove 10 peguins, essentially purchase (misspells are purposeful)

the if statement isn’t running, i thought it was because i was updating the values on the client, but idk if that’s an issue if i’m getting them on another localscript

i’ve looked at other topics and tried the developer hub but i couldnt find anything, here’s the script

local button = script.Parent
button.MouseButton1Up:Connect(function()
local ppc = button.Parent.Parent.Parent.Parent.ppc.Value
local peguins = button.Parent.Parent.Parent.Parent.peguins.Value
if peguins >= 10 then
peguins = peguins - 10
ppc = ppc + 1
end
end)

1 Like

This should work if peguins.Value is actually going up. Have you tried switching between client and server (test tab in studio) to make sure they are updating?

1 Like

from out of all things that doesn’t work in local scripts, you choose to write “If statements”.

please give us more context, and no, If statements are not to blame lmfao.

1 Like

Also instead of doing :

peguins = peguins - 10
ppc = ppc + 1

Do :
penguins -= 10
ppc += 1

1 Like

this game is completely gui based and the script that happens when you click the peguin should make the “peguins” value increase, but for some reason it’s not, even though the textlabel that displays the value is updating accordingly, it also might just be that i have like -4 iq, though

here’s the script that should update the “peguins” value

local peguin = script.Parent
local label = peguin.Parent.Parent.peguinsUI.TextLabel
local peguins = peguin.Parent.Parent.peguins.Value
local ppc = peguin.Parent.Parent.ppc.Value

local function click()
peguins = peguins + ppc
label.Text = ("peguins: "…peguins)
end

local function change()
peguin.Image = “rbxassetid://14658370275”
peguin.Sound:Play()
wait(0.5)
peguin.Image = “rbxassetid://14638046008”
end

peguin.MouseButton1Up:Connect(function()
click()
change()
end)

i’m not actually sure why it doesn’t update the values, as it works on server side and they still don’t appear to update, i only put the “if statement” in the title as it’s the thing that isn’t running (confirmed with print), and i’m not really sure why

1 Like

You are saving a copy of peguins.Value, thats why.

local peguins = peguin.Parent.Parent.peguins.Value

You actually need to do this:

local peguins = peguin.Parent.Parent.peguins --Remove .Value
local ppc = peguin.Parent.Parent.ppc --Remove .Value

local function click()
peguins.Value = peguins.Value + ppc.Value
label.Text = ("peguins: "…peguins.Value)
end

This is an extremely common mistake caused partially by the fact lua symbols do not have a way to convey whether they are references. Primitive types such as numbers are copied when you use them to assign:

local origValue = 5
local copyValue = origValue

--Changing copyValue will not change origValue

whereas an object value, such as an Instance or table, are not copied when used to assign:

local tab = {"Jeff"}
local sameTab = tab

--sameTab and tab refer to the same actual table

thanks! with a couple quick modifications it works :slight_smile:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.