Storing Tokens/ Organization like in Bee Swarm Simulator

Im making a game like Bee Swarm Simulator, and this is the first time of me going into this organization/storing side of scripting.
I wanted some ideas/solutions of how i could store such Tokens, and make it easy to potentially make it so in the future i could add Tokens, that would have special functionalities.

I’ve thought about putting all the Tokens and what they do inside a Module like a Database which is not bad but what is stored inside it is what im most confused about.
Going back to the idea of special functionalities, how would i store such functionalities, and their functions?

Do i just check if the token is “Special” and then execute their “Special” code or do i put it in a separate table of said Token Database with the “Special” Token’s Function?
I thought about using _G but i think i will only be using this Token Database from one module.
How i would store these Tokens would be something like this:
image

Each Token would also have different Types, so i could identify which Token does what easier.
I don’t know if this is the most efficient for performance/organization, so im very confused on what i could do to store things like this.
Thanks for your time.

3 Likes

If you plan to copy token’s data and store it somewhere else, then you need to make links to functionality functions, instead of storing them directly.
If you plan to just link tokens by name (NOT copying) - then your structure is good.

2 Likes

I plan on grabbing the Token’s Data directly, like in the screenshot i provided it would be something like that, then executing that Token’s base function if theres no special functions. But i would just make a check to see if it has said special functions and execute them. But how would i make such links?
You mean links, by modules sharing functions?
I didn’t understand what you mean’t by links.

1 Like

My idea was kind of like this:

function GrabToken(Token)
	local TokenFromDatabase = whateverDataBase[Token]
	if TokenFromDatabase.HasSpecialFunctionalities then
		-- Grab Function and execute it.
	end
end

Since this Function is probably only gonna pass stuff like the Token’s VFX data to the Client, this function would be completely internal to the Server.

3 Likes

I don’t remeber exactly how functions are behaving in cases like this one, but I think they are similar to tables - links.
So, make module with all functions you need, and then type smth like that:

Functionalities = {
Module.Explosion,
Module.Light,
}

Tokens.Tokens.Bomb.TestToken.Functionalities[1](Needed data)
Tokens.Tokens.Bomb.TestToken.Functionalities[2](Needed data) --put this in for loop
2 Likes

Hmm, so store all the Functions needed in a separate module, then find the said Function inside a for loop and execute it via that?
Never thought of this, i’ll implement it.
Thank you.

If i have any more questions i will come back to this post.

You a bit misunderstood for part (maybe):

  1. Make module with all functions needed. function Module.Explosion() ... end
  2. Then, link all tokens to functions you need: Module.Explosion
  3. Then, use functions you need. Cuz it will be lighter to save functions in indexed array, you need to call all functions with for loop for i = 1, #Token.Functionalities, 1 do Token.Functionalities[i]() end

So something like this?

--EXAMPLE 1
local SpecialFunctions = require(script.SpecialFunctions) --Special or just all Token Functions ?

function GrabSpecialFunction(Token)
        for i, Function in pairs(Token.SpecialFunctions() -- This would store all the Function names.
               SpecialFunction[Function](Token)  --Execute function
       end
end

--EXAMPLE 2 

local SpecialFunctions = require(script.SpecialFunctions) --Special or just all Token Functions ?

local MyTokenFunctions = {
   SpecialFunctions.Function1,
   SpecialFunctions.Function2
}

function GrabSpecialFunction(Token)
        for i, Function in pairs(MyTokenFunctions() -- This would store all the Functions.
               Function()
       end
end

I think you meant the second Example, i thought it was the first Example at first.
But if its the second Example, i would have to manually type out each Function for said Token?
I thought of it being:

   for i, Function in pairs(TokenFunctions)
          Function()
   end
  -- This would execute the Functions kind of automatically, kind of like the first example.
      

Yep, I meant 2nd Example.

Ok, by the way i meant automatic kind of like this:
image

I would just put the Function’s Name and execute it by that.
And it would utilize the for loops u suggested.

As a dedicated bee swarmer who’s worked on a game like this I must invest heavily into this post.

Although this method would work I would do it differently where it’s less confusing…

Here’s an example scenario I have carefully constructed.

Module script located in Replicated storage which stores data for all the tokens just like your other method

local tokens = {
["HoneyToken"] = {
--  unique properties
ImageId = 00000000, -- the idea of the token image
Color = color3.fromRGB(255,255,255), -- colour of token
Duration = 10, -- how long the token lasts for
Effects = {}, -- any effects
Collected = function() -- upon collection
 -- give the player 3000 honey
end
},
["Bomb"] = {
ImageId = 00000000, -- the idea of the token image
Color = color3.fromRGB(255,255,255), -- colour of token
Duration = 15,
Radius = 5
Effects = {},
Collected = function() -- upon collection
 -- destroy flowers in a 5 flower radius. I'm not sure how you have coded your system so far but you don't really need to specify the pollen 
-- if I'm not wrong. The pollen is automatically collected when the flowers are blown up you just need to make a system for it

-- if this gets too messy you can always store the functions in a module to isolate them for organisation
end
},
["RedBoost"] = {
ImageId = 00000000, -- the idea of the token image
Color = color3.fromRGB(255,255,255), -- colour of token
Duration = 15,
Effects = { -- test variables in this table
RedPollen = 1.5 -- some red pollen
Capacity = 2 -- double capcity
},
EffectDuration = 60,
Collected = function() -- upon collection
 -- send the Effect table and effect duration to the client but set the actual effects on the server for the player otherwise setting any data on the client leaves it vulnerable to exploiters
end
}
}

return tokens 

Module script located in Replicated Storage to be used by client and server

local TOKEN_DATA = require(our module with the list of tokens and data)

local token = {}

function token.SpawnToken(tokenName: string, location: Vector3) -- the token we want
 --Have a token template stored in replicatedStorage somewhere and clone it
-- apply the colour and image to the token
-- spawn the token in
token.CFrame = CFrame.new(Vector3)
end

function token.TokenCollected(tokenName) -- we will take the player arguement since we dont know if this is server or client
-- this function is fired when the token is touched, I recommend using raycasting as it's more optimised to detect when the player touches the token
-- cool token collect effect and sound
TOKEN_DATA[tokenName].Collected()
end

-- with this system you can call to spawn tokens on the client or server it doesnt matter so it's easy to have client sided tokens for your bee abilities and server sided ones.

return token

Extra stuff

To have a display of any token effects on the client you would fire to the client the effect table and effect length through the Collected function, view more of this on the RedBoost token within the token data

Don’t give me the solution for this topic the other person’s method would still work this is just my way of doing it.

Thanks for you response!
Like your idea, and i was actually making something similar.
What i plan to do and i believe is how Bee Swarm actually does it:

1 - Theres no need to put this Module in ReplicatedStorage, first of all its better to stray it from potential hackers, makes them not know your logic for this sort of mechanic since they can’t access things on the Server.

2- To replicate everything to other Clients, i will check if the other Players in the game are close to other Players and in the future if they have a setting enabled to show other Player’s Bees, then show them to that specific client.

3- I made a Module in the Server, that stores the Data of each and every single Token that i will make sort of like a DataBase but in the future i will look into the possibility of maybe storing them via Buffers, or DataStores if u have ideas on how i can store them and then load them in when the game loads then i would appreciate it.

4- I made 2 Modules for Token Functions, 1 is for “Base Functions” which most Tokens will use, and are very simple and straightforward functions, and the 2nd Module is for “Special Functions” where i would need to be more specific with what the Token will do, think of it like this:

In Bee Swarm Simulator there’s this bee called Precise Bee, which i find to be very complicated so it’s Functions would be in this Module, and let’s say if my Bees in the future get very complicated and technical i might make a new Bee Module to handle more complicated Bees. (Or something like that)

5- Your method is not bad, but i feel like, storing functions inside Tables like i was doing would get very messy like you also mentioned in your post, another good thing is that the Server won’t do heavy work, atleast i think so.
Because the Client will be doing the Animations, Player Inputs and all these other things that are basically meant for the Client.

6- In Bee Swarm Simulator, a lot of the main calculations and checks are just done on the Server and the Server mostly handles things like loading things, performing checks.

I might be wrong in a few of these so i would like to get your opinion.
I will post some code samples in a bit to get some feedback.

Another thing im also doing (I don’t know if i mentioned this properly in the previous post)
Im generalizing the Bee’s Functions, to take in basic/expected arguments, for example:

Blue Bee produces a Booster Token, and executes the “Booster” Function if the Token is picked up.
Now Red Bee produces another Booster Token, and executes the same Function if the Token is picked up.
Now to save more memory and cause less lag, imma just take in 2 arguments in the Booster Function that would be the Bee’s Color/Bee Type, and some other arguments if needed


-- TOKENS DATA BASE 

Tokens.BaseAttributes = {
	["TestToken"] = {
		TokenName = "TestToken",
		TokenDecal = "rbxassetid://17726394721",
		TokenColor = Color3.fromRGB(255, 255, 255),
		TokenType = "Bomb",
		TokenLifeTime = 10,
	}
}

Tokens.Tokens = {
	["Bomb"] = {
		["TestToken"] = { -->> TEST TOKEN
			BaseAttributes = Tokens.BaseAttributes["TestToken"], -->> Put Token Name here.
			Radius = 5, -- 1 Flower is equal to 1 Radius.
			PollenPerFlower = 5,
			HasSpecialFunctions = true,
			SpecialFunctions = {
				"Print",
			},
		},
	},	
}

Im planning on adding Base values that will just be tweaked depending on the Token later.
I have kind of the same for every other DataBase i have, but i just need to implement some Base Values.
For even easier detection, im planning to learn how to make Enums so i could make Enums for all the Token Types.

My only Function for Tokens right now is this one:

function BaseFunctions:Bomb() --
	local FlowersToBlowUp = {}
	local TokenStats = self.TokenStats
	local BeeType = self.Bee.BeeType
	local Field = self.Field -- Field the Token is at.
	local CurrentFlower = self.Flower -- Flower the Token is at.
	
	table.insert(FlowersToBlowUp,CurrentFlower)
	for i, Flower in pairs(Field.Flowers:GetChildren()) do
		if (CurrentFlower.Position - Flower.Position).Magnitude < TokenStats.Radius then
			table.insert(FlowersToBlowUp,Flower)
		end
	end
	local PollenToBeGathered = #FlowersToBlowUp * TokenStats.PollenPerFlower
	print("Pollen to be gathered: "..PollenToBeGathered.." Amount of Flowers: "..#FlowersToBlowUp)
	ReduceFlowerBindable:Fire(self.Bee.Owner,FlowersToBlowUp,PollenToBeGathered)
end

(For clarification the self variable is the Token)
I know this not the most optimized, and also using Bindable Events for this is kind of bad, but ill figure out a way to get the Client and Server to communicate this by not using Remote Events or something if that’s even possible, if u have any ideas please suggest them!

How this Bomb Function looks in-game, excuse the Bee Movement and GUI :joy:
(I don’t know why the quality is so bad)

External Media

For the Bee to be able to spawn the Token i use this random number:

local ChanceCalculations = (self.AbilityRate * 0.10) * (self.Owner.BeeAbilityRate.Value / 100)
local TokenChance = math.random((ChanceCalculations),10) 

Don’t know if its the best way of doing it though.

This video represents what i wanna do really well, with the Server to Client Communication, but im still super skeptical about ReplicatedStorage.

This tutorial has a pretty good and fast datastoring system perfect for a lot of values like bee swarm simulator.

Im not sure what you mean by this but in that case you could use a remoteFunction to get token data from the server and bring it on the client. Then just use the SpawnToken function.

I haven’t seen this video before but I use a premade module script that does all event methods.

hmm ok, have you seen my other posts? Its a lot lol

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