basically what i need this code to do is:
- when a region is called from the client, the server looks in the FishTable and gets all fish within that region
- the server rolls RNG to pick a rarity
- the server picks a fish from the selected region with the applied rarity
i’ve written the code here:
--serverscript in serverscriptservice
local RepStrg = game:GetService("ReplicatedStorage")
local RNG = require(RepStrg.Mods.WeightedRNG) -- RNG Module, all RNG is handled by the server so as to prevent exploiters
local Rarities = require(RepStrg.Mods.Rarities) -- rarities table
local Fish = require(RepStrg.Mods.FishTable) -- fish table
local CreateFish = script.Parent.CreateFish -- creates fish for GhotiMod to use (not currently being used, ignore this)
local Regions = require(RepStrg.Mods.RegionTable) -- table of all possible regions
local rollRNG = RepStrg.Events.RollRNG -- must be a RemoteFunction
local CheckRegion = RepStrg.Events.CheckRegion -- this is also a remotefunction
local selectedRegion = nil
local function GetRegionFromClient(plr, Region) -- gets the region the player's bobber lands in (region is defined with an invisible part in workspace)
print("the server received region:", Region)
selectedRegion = Region
return selectedRegion
end
CheckRegion.OnServerInvoke = GetRegionFromClient
-- this function to calculate a roll for the playeerr :DDDD
local function calculateRNG(plr: Player, RaritiesTable: { [string]: { chance: number} }, FishTable, Region: string)
-- i hope this checks it safely
if type(RaritiesTable) ~= "table" then
warn("Rarities table is nil or invalid!")
return
end
-- this gets the region from the player
local Region: string = selectedRegion
if Region then
print("The server received region:", Region)
else
warn("Region is nil or invalid")
end
-- roll the rarity
local rolled: string = RNG.RollRNG(RaritiesTable)
print(plr.DisplayName .. " Rolled: " .. rolled)
-- this picks a fish with a matching rarity and region
if FishTable and type(FishTable) == "table" then
local fishList: {any} = {}
for _, fish in pairs(FishTable.fish) do
if fish.rarity == RaritiesTable[rolled] then
table.insert(fishList, fish)
end
end
if #fishList == 0 then
print("No fish available for this rarity/region.")
else
local selectedFish = fishList[math.random(1, #fishList)]
print("Selected Fish: " .. selectedFish.name)
end
end
return rolled
end
-- this listens for the event and then calculates the roll
rollRNG.OnServerInvoke = function(plr: Player)
return calculateRNG(plr, Rarities, Fish, Region)
end

