Scripting Dilemma

Hi, So I’ve got a bit of a dilemma, I have local chests which the player is only supposed to be able to open one time. So my solution was to make the chests give you two values. 30 coins and a second value, This value would be for the system to recognize that you already opened the chest. I currently have 2 as there are only 2 chests in the game. “CHA1” and “CHA2”.

(the CH is for chest, The A is for area A and the number is pretty self explanatory)

The problem is, I want the player to be able to open them out of order. e.g. Opening CHA2 before CHA1.

But I’m not sue how this would be possible, As you can see below

while wait() do

	if data1.Value >= 1 then
			Chest:Destroy()
		
while wait() do
				
    if data2.Value >= 1 then
				Chest2:Destroy()

The second while wait() do can only fire after the first one has.

I’ve spent hours searching for solutions and tried just about every solution I could with my current skillset :pensive:

Here’s the full script:

local player = game:GetService("Players").LocalPlayer
local Chest = game.Workspace.Collectibles.CHA1
local Chest2 = game.Workspace.Collectibles.CHA2
local data1 = player:WaitForChild("collectibledata"):FindFirstChild("CHA1")
local data2 = player:WaitForChild("collectibledata"):FindFirstChild("CHA2")

while wait() do

	if data1.Value >= 1 then
			Chest:Destroy()
		
while wait() do
				
    if data2.Value >= 1 then
				Chest2:Destroy()	
			end
		end	
	end
end

If you have any questions please feel free to ask, This was quite difficult to explain so pardon if it doesn’t make much sense :sweat_smile:

Any and all help would be appreciated!

Why cant you just destroy the chests when you open them, and only run the deletion check on data load if you want to implement that? It would be far more effecient than a loop.

Also, they are forced to be in order because you need to add ends to the end of the loop

while task.wait() do

	if data1.Value >= 1 then
		Chest:Destroy()
	end
				
    if data2.Value >= 1 then
		Chest2:Destroy()
	end
end
2 Likes

You should avoid to use any type of loops for everything, as it take a lot of ressources overall, there is an optimized way to do it properly.

--//Services//--
local PlayerService = game:GetService("Players")
local WorkspaceService = game:GetService("Workspace")

--//Variables//--
local Player = PlayerService.LocalPlayer

local Collectibles = WorkspaceService:WaitForChild("Collectibles" ,300)
local Chest1 = Collectibles and Collectibles:WaitForChild("CHA1" ,30)
local Chest2 = Collectibles and Collectibles:WaitForChild("CHA2" ,30)

local CollectibleData = Player:WaitForChild("collectibledata" ,300)
local Data1 = CollectibleData and CollectibleData:WaitForChild("CHA1" ,30)
local Data2 = CollectibleData and CollectibleData:WaitForChild("CHA2" ,30)

--//Functions//--
if Data1 then
	Data1:GetPropertyChangedSignal("Value"):Connect(function()
		if Data1.Value >= 1 then
			Chest1:Destroy()
		end
	end)
end

if Data2 then
	Data2:GetPropertyChangedSignal("Value"):Connect(function()
		if Data2.Value >= 1 then
			Chest2:Destroy()
		end
	end)
end
2 Likes

Tysm! Will this also work reliably when a player joins?

No problems !
It is a client sided local script, so when chests are getting destroyed, only you will see them getting destroyed, other players still see the chests if their value are < 1.

1 Like

I hate to keep nagging, But it currently only checks when the value is changed, Do you know how I would go about making it check when you join the game too?

(Update: I’ve figured it out)

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