How to prevent this

i am trying to make a shop but is there a way to prevent when the player clicks the previous button and it wont throw an error like this

16:27:31.458 - Episode 0 is not a valid member of Folder
16:27:31.461 - Stack Begin
16:27:31.462 - Script 'ServerScriptService.Server-Scripts.Scripts.ChangePosition', Line 24
16:27:31.463 - Stack End

here is the code [CURRENT]

game.ReplicatedStorage["ReplicatedStorage Stuff"]["RemoteFunctions"].Call.OnServerInvoke = function(plr, String)
	if String == "Increase" then
		local PlayerGui = plr:WaitForChild("PlayerGui")
		local ShopInfo = PlayerGui:WaitForChild("ShopInfo")
		local CostName = ShopInfo.EpisodeCost.EpisodeName
		local EpisodeName = ShopInfo.EpisodeName.EpisodeName
		plr.Stats.Values.CurrentNumber.Value = plr.Stats.Values.CurrentNumber.Value + 1
		local EpisodeValue = plr.Stats.Season1["Episode "..plr.Stats.Values.CurrentNumber.Value]
		if EpisodeValue.Value == true then
			ShopInfo.Status.TextButton.Text = "Play!"
		elseif EpisodeValue.Value == false then
			ShopInfo.Status.TextButton.Text = "Buy!"
		end
		CostName.Text = workspace.Background.ShopStuff["Episode "..plr.Stats.Values.CurrentNumber.Value].Values.Cost.Value.." Coins"
		EpisodeName.Text = workspace.Background.ShopStuff["Episode "..plr.Stats.Values.CurrentNumber.Value].Values["Episode Name"].Value
		plr.Stats.Values.CurrentEpisode.Value = "Episode "..plr.Stats.Values.CurrentNumber.Value
		return "Episode "..plr.Stats.Values.CurrentNumber.Value
	elseif String == "Decrease" and plr.Stats.Values.CurrentNumber.Value <= 2 then
		local PlayerGui = plr:WaitForChild("PlayerGui")
		local ShopInfo = PlayerGui:WaitForChild("ShopInfo")
		local CostName = ShopInfo.EpisodeCost.EpisodeName
		local EpisodeName = ShopInfo.EpisodeName.EpisodeName
		plr.Stats.Values.CurrentNumber.Value = plr.Stats.Values.CurrentNumber.Value - 1
		local EpisodeValue = plr.Stats.Season1["Episode "..plr.Stats.Values.CurrentNumber.Value]
		if EpisodeValue.Value == true then
			ShopInfo.Status.TextButton.Text = "Play!"
		elseif EpisodeValue.Value == false then
			ShopInfo.Status.TextButton.Text = "Buy!"
		end
		CostName.Text = workspace.Background.ShopStuff["Episode "..plr.Stats.Values.CurrentNumber.Value].Values.Cost.Value.." Coins"
		EpisodeName.Text = workspace.Background.ShopStuff["Episode "..plr.Stats.Values.CurrentNumber.Value].Values["Episode Name"].Value
		plr.Stats.Values.CurrentEpisode.Value = "Episode "..plr.Stats.Values.CurrentNumber.Value
		return "Episode "..plr.Stats.Values.CurrentNumber.Value
	end
end

I believe the problem is with the finding of the “Episode” object, therefore, I would recommend using

:WaitForChild()

to prevent the error from happening.

its not there and will always be not there

its the minus issue

I’m blind or what but when you want to move things in the GUI you use Udim2. Quick example: player.PlayerGui.TestGUI.TextLabel.Position = Udim2.new(0,0,0,0)

the issue is the minus how can i check if the value ~= 0

I will reply, but I have to read the code again.

I believe this is line 24, so maybe try doing:
local EpisodeValue = plr.Stats.Season1["Episode "..plr.Stats.Values:WaitForChild("CurrentNumber").Value]

thats not the issue

the issue is this

plr.Stats.Values.CurrentNumber.Value = plr.Stats.Values.CurrentNumber.Value - 1

That’s not where this error comes from, in the error he specified you can see that it found 0.

You can put i.e.

plr.Stats.Values.CurrentNumber.Value = plr.Stats.Values.CurrentNumber.Value - 1
if plr.Stats.Values.CurrentNumber.Value <= 1 then
plr.Stats.Values.CurrentNumber.Value = 1
end
1 Like

Is this a line when problem happens? Maybe this will work:
local EpisodeValue = plr.Stats.Season1[“Episode”]…plr.Stats.Values.CurrentNumber.Value

He’s trying to find out how to make the script check whether the value will be larger than 0 after he removes 1 from it since he deliberately doesn’t have “Episode 0” in the folder he’s looking for.

that works

30char30char

When helping other people the problem for me and others is that you didn’t make the code that guys want a help with. I mean it’s difficult to exactly know how everything works from a small piece of code in only one button.

Everything works nice job, @VirtualButFake!