Bumping topic again (30 lettersssssssss)
Sorry, I’m struggling to create a working script. I’m very bad with with math and probability. I imagine a line cut into different segments, where the line size is 1, and the segements size is the chance of each ore. And then a random number between 0 and 1 would be plotted, on the number line, and which ever segment it hits is the ore you obtained. I’ll try to create a working script in the meantime.
Okay thank you a lot (30 letterssssssssssss)
I tested this, it seems fine.
local chanceTable = {
Plutonium = 0.5,
Diamond = 0.1,
Coal = 0.35,
Stone = 0.35,
}
local function GetOreObtained()
local segments = {}
local total = 0
for i, v in pairs(chanceTable) do
table.insert(segments, {total, total + v, i})
total += v
end
local depth = 1000 -- more depth allows for more preciseness for chance, but takes longer to compute
-- 1000 depth allows for chances like: 0.0001.
local x = math.random(0,depth) / depth
for _, seg in pairs(segments) do
if seg[1] <= x and x <= seg[2] then
return seg[3]
end
end
return "Stone"
end
If the values in chanceTable add up to 1, everything should work. If they go over 1, some chances might change unexpectedly. If they go under 1, the chance of Stone increases. You can replace chanceTable
with your chancesTable
to make it work with your script.
Alright ty let me try it, btw where you return “Stone” in last stroke? Just because i want to change attribute with that value, should i add like
...GetOreObtained(material)
and then set attribute in regenerate script?
“Stone” is just the default ore incase an error happens.
You mean a nil error which says about nil value? Because it was appearing a lot times in my script, which was breaking whole cluster script .
Sure. If it can’t calculate chance, the ore is nil, so it returns Stone by default.
Lol you fixed two of my errors i had, one about highest chance second about nil thing, thank you. And btw now how i should i set thing from this table to attribute
I changed the default to “Unknown”, and printed the result of the function 10 times. It never printed unknown, it just printed random ores respective of their chance of being obtained.
What do you mean by this? The function returns an ore’s name.
But function have no args in it…? or i’m dumb, just don’t understand where and what it returns
EDIT: Ohh i’m dumb i could use
Cluster:SetAttribute("Material", GetOreObtained())
am i right?
EDIT: Well seems that thing works, let me quick add server luck multiplier to it
In this part of the code, it tests to see if x is inside of the segment. If it is, it returns the name of the ore, which is the third value of the segment.
If it never returns anything in that loop, the function returns the default “Stone”. The function does not need any arguments because it accesses chancesTable
directly.
Hmmm just curious now, where should i put my server luck boost to this script.
here or no?
local x = math.random(0,depth) / depth * Boost
Adding a luck booster requires some extra work, believe or not. These segments are not sorted by size, they’re actually randomly placed. With no luck booster, this is fine. But multiplying the x without sorting the segments by size will actually make some unpredictable results that the player won’t like. To make this work, you need to sort segments from least to greatest, and then multiply x by a number lower than 1 to guarantee better chances for smaller segments. I’ll modify the script to do this, give me a few minutes.
Okay thank you a lot, i appreciate that (30 letterssss)
I think this works. If you increase the luck booster value, Diamond shows up a lot more.
local chanceTable = {
Plutonium = 0.5,
Diamond = 0.1,
Coal = 0.35,
Stone = 0.35,
}
-- Convert chanceTable to an array to be sorted
local ores = {}
for i, v in pairs(chanceTable) do
table.insert(ores,{i,v})
end
table.sort(ores, function(a, b)
return a[2] < b[2]
end)
-- Sort chanceTable into segments
local segments = {}
local total = 0
local luckboost = 1 -- increase value to increase luck
for _, data in pairs(ores) do
table.insert(segments, {total, total + data[2], data[1]})
total = total + data[2]
end
local depth = 1000 -- more depth allows for more preciseness for chance, but takes longer to compute
-- 1000 depth allows for chances like: 0.0001.
local function GetOreObtained()
local x = math.random(0,depth) / depth
x = x / luckboost -- forces x to be closer to smaller segments
for _, seg in pairs(segments) do
if seg[1] <= x and x <= seg[2] then
return seg[3]
end
end
return "Unknown"
end
for i = 1, 10 do
print(GetOreObtained())
end
Uhh, maybe i’m just very very very lucky, but bruh it chance is 0.001565(Stellarite), let me check few more times
Okay nvm i just upgraded luck and now it spawns the best ore always(to 34 Mx, so it is normal) Thank you for the help, you are really nice person
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.