#logic problem

1 messages · Page 1 of 1 (latest)

ruby ridge
#

If i have this call to a module

local LeaderstatsModule = require(game.ServerScriptService.Modules.Game.Leaderstats)
    
Players.PlayerAdded:Connect(function(player)
  self:_loadData(player)        
  LeaderstatsModule:Set(player, self.PlayerData[player.UserId])
end)

function Leaderstats.Set(self: Leaderstats, player: Player, data: { Coins: number, Wins: number }, onChange)
    local leaderstatsClone = leaderstatsFolder:Clone()
    leaderstatsClone.Parent = player
    
    Coins.Value = data.Coins
    Wins.Value = data.Wins
    
    Coins.Changed:Connect(function(value)
        --stuck here
    end)
end

How do I make it to call a function from the data service without having to require in both modules since it gives a type error

wild plaza
#

Maybe you could have a
(Might be a bad example, on the phone rn and cant really look at it)
Datastorage.module and a leaderboard.module,

Require both in a handler module

#

En go on there

#

I mostly try to refrain from modules requiring other modules, unless they are helpers for that class

ruby ridge
#

My service initialization is by looping

#
local ServicesFolder = script.Parent

--[[
    Load and initialize all services
]]
for _, v in ipairs(ServicesFolder:GetDescendants()) do
    if v:IsA("ModuleScript") then
        local service = require(v)
        
        service:Init()        
    end
end
surreal gyro
#

so under .Changed you would just callback()

#

or other way around

#

am too lazy to think

wild plaza
#

Sometimes you should change your approach of doing things

surreal gyro
#

Wait you already got onChange argument

#

which i dont think youre using?

#
function Leaderstats.Set(self: Leaderstats, player: Player, data: { Coins: number, Wins: number }, onChange)
    local leaderstatsClone = leaderstatsFolder:Clone()
    leaderstatsClone.Parent = player
    
    Coins.Value = data.Coins
    Wins.Value = data.Wins
    
    Coins.Changed:Connect(function(value)
       onChange(Coins)
    end)
end
#

smth like that?

#

Assuming youre not using onChange argument already, which it seems like you aint?

#

@ruby ridge

wild plaza
#

local DataService = require(script.Parent.DataService)
local Leaderstats = require(script.Parent.Leaderstats)
function InitServices()
DataService:Init()
Leaderstats:Init(DataService) -- pass dependency explicitly
end

return {
Init = InitServices
}


function Leaderstats:Init(DataService)
self.DataService = DataService
end

function Leaderstats:Set(player, data)
local leaderstatsClone = leaderstatsFolder:Clone()
leaderstatsClone.Parent = player

local Coins = leaderstatsClone:FindFirstChild("Coins")
local Wins = leaderstatsClone:FindFirstChild("Wins")

Coins.Value = data.Coins
Wins.Value = data.Wins

Coins.Changed:Connect(function(newValue)
    self.DataService:UpdateCoins(player, newValue)
end)

end

#

That would've beeb my approach

wild plaza
surreal gyro
#

Just to be clear, in your original code you already had OnChange argument defined which i dont think you were using

#

so just make sure i didnt break shit

#

by using it

ruby ridge
#

sorry ima be reading in a moment

ruby ridge
#

Coder, is there anything im doing wrong in the code flow?

#

Like

#

Idk man, maybe the way im writting the code is not the best so thats why im getting issues

winter holly
ruby ridge
#

The most issue is maybe because of the way i am doing my code it doesnt work

winter holly
#

idk how to do data stuff

ruby ridge
winter holly
ruby ridge
#

Not roblox services

#

Created services

#

Services that you create and initialize

silver bough
silver bough
#

require(dataserver.types_script) require(leaderstats.types_script)

#

like that

ruby ridge
#

yes but the actual issue is not about types

silver bough
#

or yeah restructure so only one side needs the types

ruby ridge
#

is about service A needing module B and module B needing service A

#

It gives some memory cycle error

silver bough
#

ye recursive require

ruby ridge
#

And the .Changed event isnt firing

silver bough
ruby ridge
#

Mostly right now I just want to know how to evade this for next time

ruby ridge
#

Id have to do it in manual way

limpid mica
#

use profile store rir

#

sir

ruby ridge
#

ik i can google it but

#

i want the answer from you actually

wild plaza
mental nymphBOT
#

studio** You are now Level 7! **studio

limpid mica
#

but its a data store module

#

its very good

#

id rather u use that

surreal gyro
#

@ruby ridge still issue? just got home

wild plaza
#

It musnt be big tho

#

Like just Setters & Getters ex

#

I have created my own a while ago

ruby ridge
#

i think i got the solution just want to be sure

#

instead of making the 2 modules require each other

#

i put all the logic in the loader

#

so i dont make them to require

surreal gyro
#

What isnt working tho

#

.Changed?

ruby ridge
#

atp

#

i dont think the way i was doing it is a correct one

#

it may work

#

but its not efficent

#

im trying to make this scalable

#

so what do you think instead of making DataService and leaderstats have connections like .Changed and PlayerAdded

#

I just make the functions

#

And do the playeradded and changed logic