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

[SOLVED] Random chest spawning, the chest spawns but then why it doesn't spawn another one?

I'm making a random chest spawning spot but there's a problem. Here's the code:

local treasure = script.Parent.Treasure.Value
local common = game.Lighting.Chests.CommonChest1
local uncommon = game.Lighting.Chests.UncommonChest1
local rare = game.Lighting.Chests.RareChest1
local epic = game.Lighting.Chests.EpicChest1
local legendary = game.Lighting.Chests.LegendaryChest1
local chances = {common,common,common,common,common,common,common,common,common,common,uncommon,uncommon,uncommon,uncommon,uncommon,rare,rare,rare,epic,epic,legendary}

while wait(10) do
	if treasure == false then
	treasure = true
	chest = chances[math.random(1,#chances)]:Clone()
	chest.Parent = script.Parent
	end
end

When the player takes the chest the treasure value is set back to false so that's not a problem, but still it doesn't spawn another chest. Can anybody help please?

2 comments

0

You're not setting the treasure value back to false after it spawns.

0

The treasure value does go back after the chest is taken but even after that it doesn't spawn

2 answers

✓ Accepted answer

You've set the treasure variable to true but forgot to change it back to false so it can run what's in the if statement again.

12 comments

0

No, even after that it doesn't work. After the player takes the chest the treasure value goes back to false but even then it doesn't work and I don't know why.

0

Can I see the code that sets it back to false?

0

Yes. I put it.

0

Regular scripts.

0

I'm not sure, Try printing something inside the if statement in the chest's code and see if it works when you touch a chest

0

Yes I tried that, and even before trying that I knew the chest worked but the spawning script doesn't work.

0

I found the issue I think, You set treasure variable to the bool value of the treasure at the time. So when you later reference it, it uses the value of treasure from earlier. - Hope this isn't confusing lol

0

So get rid of 'local treasure = script.Parent.Treasure.Value' and use 'if script.Parent.Treasure.Value == false then' instead of 'if treasure == false then'

0

Don't bother anymore, thanks for helping me. I fixed it, I fused the chest's script with the spawning script and changed it a bit and it works now.

0

It would be good if you understood why for the future: It works now because it is sharing the treasure variable instead of the Treasure Bool Value from different times (read the other two replies if confused).

0

Yes I did understand your solution. Thanks for helping.

0

No problem, if you set my answer to solution it will show as solved for other people, And also give me reputation lol

The chest's code:

local box = script.Parent.HitBox
local coinsreward = script.Parent.Configuration.CoinsReward.Value
local expreward = script.Parent.Configuration.ExpReward.Value


box.Touched:Connect(function(part)
	local parent = part.Parent
	local humanoid = parent:FindFirstChild("Humanoid")
	local player = game.Players:GetPlayerFromCharacter(parent)
	
	if humanoid ~= nil and player then
		player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + coinsreward
		player.leaderstats.Exp.Value = player.leaderstats.Exp.Value + expreward
		script.Parent.Parent.Treasure.Value = false
		game:GetService("Debris"):AddItem(script.Parent,0)
	end
end)

6 comments

0

I recommend setting it back to false after it spawns, which is in the spawning function. Because when you are changing the parent of the box, so you're also changing the directory to the value.

0

But I need that treasure value to be only set to false after the chest was taken because that value is there to prevent the chest being spammed.

0

Well, change the directory of the treasure value in the chest code to match where it's actually at, otherwise it won't work.

0

Don't bother anymore, thanks for the answers and help. I fused the chest's script with the spawning script and changed it a bit and it works now.

0

Alright, glad it was solved somehow!

0

I explained why (i think) it works now, and the solution for ig earlier if you wanna find out. - it's in the comments of my answer

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