Is there a way to make this RNG be region dependant?

basically what i need this code to do is:

  1. when a region is called from the client, the server looks in the FishTable and gets all fish within that region
  2. the server rolls RNG to pick a rarity
  3. 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

Could you elaborate further? It sounds (and looks) like you essentially did that, though the way you’re getting the player’s region and etc. seems a little uncouth

I need the server to check what fish can be caught within a specified region, and only pick those fish within the region. Instead it picks from all possible fish defined in FishTable, even ones that aren’t supposed to be caught within the region.

Below is a list of all possible fish as defined in FishTable. In theory, if the player’s bobber lands in the “PelicanIslandOcean” region, they should only be able to catch the Garibaldi and Pufferfish. however, they can also catch the Red Snapper and Lingcod even though those fish don’t belong to that specific region. It seems like the server doesn’t know which fish belong to which region, which causes the server to select fish that don’t belong to the region the client specified.

FishTable.fish = {
	["Pufferfish"] = {
		name = "Pufferfish",
		rarity = Rarities.Uncommon,
		location = FishTable.Regions["PelicanIslandOcean"],
		basePrice = 15,

		minWeight = 4,
		maxWeight = 11,
		model = RepStrg.Assets.Fish.Pufferfish,
	},
	["Lingcod"] = {
		name = "Lingcod",
		rarity = Rarities.Uncommon,
		location = FishTable.Regions["AllOcean"],
		basePrice = 40,

		minWeight = 30,
		maxWeight = 60,
		model = RepStrg.Assets.Fish.Lingcod,
	},
	["Garibaldi"] = {
		name = "Garibaldi",
		rarity = Rarities.Uncommon,
		location = FishTable.Regions["PelicanIslandOcean"],
		basePrice = 20,

		minWeight = 0.8,
		maxWeight = 2,
		model = RepStrg.Assets.Fish.Garibaldi,
	},
	["RedSnapper"] = {
		name = "Red Snapper",
		rarity = Rarities.Common,
		location = FishTable.Regions["AllOcean"],
		basePrice = 10,

		minWeight = 7,
		maxWeight = 15,
		model = RepStrg.Assets.Fish.RedSnapper,
	},
}

debug fishlist. print all the fish values that it gets. If it’s grabbing all the fish in the table then that’s where your problem lies:

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

i don’t understand where specifically you want me to put the print command

It doesn’t seem like you’re doing anything with the Region argument, shouldn’t you filter the FishTable by the fish location property?

that’s basically what im trying to do

Then you can just add a check for it in the loop

for _, fish in pairs(FishTable.fish) do
	local isInRegion = Regions[Region] == fish.location
	if fish.rarity == RaritiesTable[rolled] and isInRegion then
		table.insert(fishList, fish)
	end
end

that seems to work, but now the server can’t seem to find any fish.
sdfsdfsedfs

After re-reading your setup, try this, if it doesn’t work I’ll need some more info about how regions are stored

for _, fish in pairs(FishTable.fish) do
	local isInRegion = FishTable.Regions[Region] == fish.location
	if fish.rarity == RaritiesTable[rolled] and isInRegion then
		table.insert(fishList, fish)
	end
end
2 Likes

this worked perfectly!!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.