Price not changing

Hi! So, I installed a shop system, and it originally worked. After implementing a use for it, though, the price stopped changing. I don’t know why! It was originally working, but all of a sudden it just stopped. Please help!

Script
--buyScript (LocalScript)
local RepStorage = game:GetService("ReplicatedStorage")
local BuyTimeEvent = RepStorage:WaitForChild("BuyTimeEvent")
script.Parent.MouseButton1Click:Connect(function()
	BuyTimeEvent:FireServer()
end)
--BuyTimeScript (Script)
local RepStorage = game:GetService("ReplicatedStorage")
local SStorage = game:GetService("ServerStorage")
local StarterGui = game:GetService("StarterGui")

local TimePrice = SStorage:WaitForChild("TimePrice")
local TimePriceLabel = StarterGui.BuyTime:WaitForChild("TimePriceLabel")
local BuyTimeEvent = RepStorage:WaitForChild("BuyTimeEvent")
local module = require(RepStorage:WaitForChild("ModuleScript"))

BuyTimeEvent.OnServerEvent:Connect(function(player)
	local leaderstats = player.leaderstats
	local cornStat = leaderstats:WaitForChild("corn")
	local timeupgStat = leaderstats:WaitForChild("timeupg")
	if cornStat.Value >= TimePrice.Value then
		cornStat.Value = cornStat.Value - TimePrice.Value
		TimePrice.Value = TimePrice.Value + TimePrice.Value * 0.1
		timeupgStat.Value = timeupgStat.Value + 1
		TimePriceLabel.Text = tostring(TimePrice.Value) .. " Corn"
		module.waitTime = (module.waitTime * 0.1 - module.waitTime) * -1
		print(module.waitTime)
	else
		print("broke")
	end
end)

Thank you!

1 Like

Can you specify which variable or instance value is not changing?

Does “Broke” get outputted? If so, then the issue is that your corn is less than time price. You need to increase corn is fix this issue.

If broke is not outputted, then the issue might TimePrice.Value. Is it a number object or an int object? If it is an int object, then what could be happening is that if TimePrice is equal to 1, your code does 1 + 1 * 0.1, which is equal to 1.1. However since TimePrice is an int object, it could be rounding down the number. This causes it to be set back to 1, which results in no change in price.

The fix is to change TimePrice to a number value object. Int only allows for whole numbers, while number allows for decimal.

Note: I could not find info on what happens when you attempt to set a decimal number to a IntValue object. However in other languages it usually rounds down or up.

I changed the IntValue to a NumberValue, yet it still doesn’t work.

There’s a textlabel on my shop board, and it says something like “100 Corn”
When you buy it, it should change to something like “110 Corn”
Buy it again, it says “121 Corn”
and so on…

This is about where it stops working

What is the value of TimePrice.Value

You are using StarterGui, but when the player joins it moves to player.PlayerGui

1 Like

Wait so if ur doing TimePrice.Value + TimePrice.Value * 0.1 and lets say (110+100)*.1 you get 21. But what is the value of TimePrice.Value

He is also right. You are changing the TextLabel on the server, not on the client.

1 Like

Your calculation is wrong here (BODMAS applied here, multiplication goes first). So it should be:
100 + 100 * 0.1, which is the same as 100 + (100 * 0.1). This results In 110.

Based on the code, TimePrice appears to be the price of something that you pay with corn. So “100 Corn” is 100 TimePrice. CornStat on the other hand appears to be how much corn the player has.

In other words, CornStat is like player cash while TimePrice is the price of an item. The price of the item increases after each purchase (by 10%, hence the *0.1).

(This is a guess as to what the code is trying to do)

@noahrepublic seems to be correct here. The fix to this is to use remote events.

  1. What you need to do is store the players corn stat and time price on the server (I suggest using tables here)

  2. When BuyTimeEvent is triggered, you will need to update the players corn stat and time price on the server script.

  3. Then the server script will need to “fire” to a remote event to update the clients GUI (In the parameters you need to include the new corn stat and total price).

  4. Then in the local script you will need to “listen”/set up OnClientEvent for that same remote event. This will then receive the new corn stat and time price.

  5. Finally you set the time price label (and corn stat label if you have one).

1 Like

I understand the order of operation, but the reason I put () around 110+100 is it equals 2.1. Since to get from 100 → 110 they added 10 corn. So what I think happened was they this (110+100)*.1 then multiplied by 100 to get 21. Then added it to 100. So 100+21 → 121 Corn

Literally just said that in my last post

Oh, so i’d need to send the info back after changing the stats? How would I put the corn stst and time price in the parameters?

Can you send the game file? So i can a solution

Sorry, I kinda want to actually have a solution and not just let someone edit the game for me, I want to do it myself

1 Like

How would I send the corn stat and price through the remote event? I’m not really good at those
Should I send it like this?

--Script
BuyTimeEvent:FireClient(player,TimePrice,cornStat)
--LocalScript
BuyTimeEvent.OnClientEvent:Connect(function(player,TimePrice,cornStat)
	print("idk")
end)

.OnClientEvent the first parameter will not be player it will be TimePrice. The reason you have player in ur server script is that the server needs to know what client/player to send the data. So ur local script should say

BuyTimeEvent.OnClientEvent:Connect(function(TimePrice,cornStat)
	print(TimePrice)
	print(cornStat)
end)

TimePrice prints nil when I test it, why is that?

This is probably TimePrice in the server hasn’t been assigned a value.

Try this in the server script
BuyTimeEvent:FireClient(player,110,1)

Why would I do that? That’d break if the player tried to buy the same upgrade twice.