#how do i add Chance to this?

1 messages · Page 1 of 1 (latest)

desert grail
#

So i have a script that generates dungeon but the right turn and left turn part have the same chance to sawn as a part that goes straight forward how can i make it that turn part have less chance to be chosen

#

Script:```lua
local Room = {}
Room.Random = Random.new()

function Room.Generate(PrevRoom)
local PossibleRooms = game.ServerStorage.StraightRooms:GetChildren()
local RandomRoom = PossibleRooms[Room.Random:NextInteger(1, #PossibleRooms)]
local newRoom = RandomRoom:Clone()

newRoom.PrimaryPart = newRoom.Start
newRoom:PivotTo(PrevRoom.End.CFrame)
newRoom.End.Transparency = 1
newRoom.Start.Transparency = 1

newRoom.Parent = workspace.DungeonParts

return newRoom

end

return Room

#

parts inside folder

mortal birch
#

well add weight, or a table that defines their chances

desert grail
#

can you give example

mortal birch
#

ok give me a min

desert grail
#

ok

mortal birch
#

can you predefine every room?

desert grail
#

wdym?

#

yes

mortal birch
#

forget it i found a better solution

desert grail
#

ok..

mortal birch
#

add a attribute to each model

#

which is their chance

desert grail
#

oh ok

#

like int value?

#

OH atribute

#

this

mortal birch
#

yes

#

but put it as a number

#

not a string ,okay

#
Room.Random = Random.new()

function Room.Generate(PrevRoom)
    local PossibleRooms = {}
    for i,v in pairs(PossibleRooms) do
        for I = 1,v:GetAttribute("Chance") do
            table.insert(PossibleRooms,v)
        end
    end
    
    local RandomRoom = PossibleRooms[Room.Random:NextInteger(1, #PossibleRooms)]
    
    local newRoom = RandomRoom:Clone()
    newRoom.PrimaryPart = newRoom.Start
    newRoom:PivotTo(PrevRoom.End.CFrame)
    newRoom.End.Transparency = 1
    newRoom.Start.Transparency = 1
    newRoom.Parent = workspace.DungeonParts
    return newRoom
end

return Room
desert grail
mortal birch
#

put the attribute name instead of "Chance"

desert grail
#

so it inserts chance value inside table?

mortal birch
#

yes kinda

#

but don't put the value like this

#

0.1 or 1%

desert grail
#

so like 1-100%

mortal birch
#

yes without the %

desert grail
#

yes

mortal birch
#

try it and tell me

desert grail
#

ok

#

16:44:54.560 ServerScriptService.Script.ModuleScript:14: attempt to index nil with 'Clone' - Server - ModuleScript:14

mortal birch
#

ok give me a second to look into this

desert grail
#

you forgot to get the rooms from the Rooms File i think

mortal birch
#
    for i,v in pairs(game.ServerStorage.StraightRooms:GetChildren()) do
        for I = 1,v:GetAttribute("Chance") do
            table.insert(PossibleRooms,v)
        end
    end
#

yes i did forget indeed

desert grail
#

it works

mortal birch
#

Yay🥳

desert grail
#

but

#

can you explain your script better

#

i dont understand it

mortal birch
#

okay so

#

i made a loop that adds the room the amount of chances it has

#

in a table

desert grail
#

i see now

mortal birch
#

oh so you understand, if you want further explanation feel free to ask

desert grail
#

ok

mortal birch
#

for example if the chances are
left = 25
right = 30
long = 50
small = 40

#

the left room has a 25 / the sum of all of the table
chance of generating

#

which is 25 / 145

desert grail
#

ohhh

#

very good

#

thank you