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

Why does the value change but the cost doesn't?

If I just leave the value at 17 or in the script put cost.Value = Max.Value *2 or something like that it will work. But for some reason it will not work like this. The original cost is 17 and the value changes but the cost stays the same. Help please

Button script and cost script

local player = game.Players.LocalPlayer


script.Parent.MouseButton1Click:Connect(function()
	local upgrade = game.ReplicatedStorage.Functions.UpgradeBackpack:InvokeServer()
	if upgrade == false then
		script.Parent.Text = "You need more coins!"
		wait(1)
		script.Parent.Text = "Upgrade"
	elseif upgrade == true then
		script.Parent.Text = "Upgraded!"
		wait(1)
		script.Parent.Parent.Parent.Parent.Parent.Parent.Visible = false
		script.Parent.Text = "Upgrade"
		local cost = player.UpgradeCosts.BackpackUpgrade
	   if player.backpack.WeightLevel1.Value == 2 then
                cost.Value = 20
                player.backpack.Max.Value = 80
            player.backpack.NextUpgrade1.Value = 160
end
    if player.backpack.WeightLevel1.Value == 3 then
                cost.Value = 21
                player.backpack.Max.Value = 160
            player.backpack.NextUpgrade1.Value = 200
end
    if player.backpack.WeightLevel1.Value == 4 then
                cost.Value = 22
                player.backpack.Max.Value = 200
            player.backpack.NextUpgrade1.Value = 210
end
    if player.backpack.WeightLevel1.Value == 5 then
                cost.Value = 23
                player.backpack.Max.Value = 210
            player.backpack.NextUpgrade1.Value = 230
end
    if player.backpack.WeightLevel1.Value == 6 then
                cost.Value = 24
                player.backpack.Max.Value = 230
            player.backpack.NextUpgrade1.Value = 250
end
    if player.backpack.WeightLevel1.Value == 7 then
                cost.Value = 25
                player.backpack.Max.Value = 250
            player.backpack.NextUpgrade1.Value = 300
end
    if player.backpack.WeightLevel1.Value ==8 then
                cost.Value = 26
                player.backpack.Max.Value = 300
            player.backpack.NextUpgrade1.Value = 301
end
    if player.backpack.WeightLevel1.Value == 9 then
                cost.Value = 27
                player.backpack.Max.Value = 301
            player.backpack.NextUpgrade1.Value = 302
end
    if player.backpack.WeightLevel1.Value == 10 then
                cost.Value = 28
                player.backpack.Max.Value = 302
            player.backpack.NextUpgrade1.Value = 303
end
    if player.backpack.WeightLevel1.Value == 11 then
                cost.Value = 29
                player.backpack.Max.Value = 303
            player.backpack.NextUpgrade1.Value = 304
end
    if player.backpack.WeightLevel1.Value == 12 then
                cost.Value = 30
                player.backpack.Max.Value = 304
            player.backpack.NextUpgrade1.Value = 305
end
    if player.backpack.WeightLevel1.Value == 13 then
                cost.Value = 31
                player.backpack.Max.Value = 305
            player.backpack.NextUpgrade1.Value = 306
end
    if player.backpack.WeightLevel1.Value == 14 then
                cost.Value = 32
                player.backpack.Max.Value = 306
            player.backpack.NextUpgrade1.Value = 307
end
    if player.backpack.WeightLevel1.Value == 15 then
                cost.Value = 33
                player.backpack.Max.Value = 307
            player.backpack.NextUpgrade1.Value = 308
end
    if player.backpack.WeightLevel1.Value == 16 then
                cost.Value = 34
                player.backpack.Max.Value = 308
            player.backpack.NextUpgrade1.Value = 309
end
    if player.backpack.WeightLevel1.Value == 17 then
                cost.Value = 35
                player.backpack.Max.Value = 309
            player.backpack.NextUpgrade1.Value = 310
end
    if player.backpack.WeightLevel1.Value == 18 then
                cost.Value = 36
                player.backpack.Max.Value = 310
            player.backpack.NextUpgrade1.Value = 311
end
    if player.backpack.WeightLevel1.Value == 19 then
                cost.Value = 37
                player.backpack.Max.Value = 311
            player.backpack.NextUpgrade1.Value = 312
end
		if player.backpack.WeightLevel1.Value == 20 then 
			script.Parent.Text = "MAX!"
	script:Destroy()
	end
end
end)

upgrade script

UpgradeBackpack.OnServerInvoke = function(player)
	local cost = player.UpgradeCosts.BackpackUpgrade
			
	if cost.Value <= player.leaderstats["Coins"].Value then
			player.leaderstats["Coins"].Value = player.leaderstats["Coins"].Value -cost.Value 
			player.backpack.WeightLevel1.Value = player.backpack.WeightLevel1.Value + 1
			return true
	else
		return false
	end
end

Thanks

2 comments

0

How do I make the 'Upgrade Script' detect when the value is being updated

0

I don't understand what the question you're asking is, but if you want the 'Upgrade Script' to detect when the value is updated, you could use the 'Changed' event from the ROBLOX API. You can read about it here: https://developer.roblox.com/en-us/api-reference/event/Instance/Changed

1 answer

✓ Accepted answer

Your question is a bit confusing,

I cleaned up your script a bit so you won't need those if-statements anymore by using a table for the backpackmaxweight outcomes. Use the ':Changed' event as Loughdough mentioned to do something when a property of an instance changes.

local player = game.Players.LocalPlayer
--You can get rid of all of those if-statements by using a table:
BackpackMaxWeights = {80,160,200,210,230,250,300,301,302,303,304,305,306,307,308,309,310,311}

--... (Other stuff with button and such)

player.backpack.Max.Value.Changed:Connect(function()
	local level = player.backpack.WeightLevel1.Value
	player.backpack.Max.Value = BackpackMaxWeights[level]
	player.backpack.NextUpgrade1.Value = BackpackMaxWeights[level + 1]
	if BackpackMaxWeights[level + 1] == nil then
		script.Parent.Text = "MAX!"
	end
end)

I hope you can maybe take inspiration from this script to possible solve your problem. I hope I have helped you.

2 comments

0

I have accepted your answer considering i answered my own question already. You just have heloed me organize better

0

Okay good!

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