Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago
Edited 4 years ago

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?

0
You're not setting the treasure value back to false after it spawns. killerbrenden 1537 — 4y
0
The treasure value does go back after the chest is taken but even after that it doesn't spawn Pokogentil 2 — 4y

2 answers

Log in to vote
0
Answered by
CyDave 42
4 years ago
Edited 4 years ago

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.

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. Pokogentil 2 — 4y
0
Can I see the code that sets it back to false? CyDave 42 — 4y
0
Yes. I put it. Pokogentil 2 — 4y
0
Regular scripts. Pokogentil 2 — 4y
View all comments (8 more)
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 CyDave 42 — 4y
0
Yes I tried that, and even before trying that I knew the chest worked but the spawning script doesn't work. Pokogentil 2 — 4y
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 CyDave 42 — 4y
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' CyDave 42 — 4y
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. Pokogentil 2 — 4y
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). CyDave 42 — 4y
0
Yes I did understand your solution. Thanks for helping. Pokogentil 2 — 4y
0
No problem, if you set my answer to solution it will show as solved for other people, And also give me reputation lol CyDave 42 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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)
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. killerbrenden 1537 — 4y
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. Pokogentil 2 — 4y
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. killerbrenden 1537 — 4y
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. Pokogentil 2 — 4y
View all comments (2 more)
0
Alright, glad it was solved somehow! killerbrenden 1537 — 4y
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 CyDave 42 — 4y

Answer this question