Hi. New to using ModuleScripts, of which I just watched a YT video on. I understand they can be used to create functions or as dictionaries (from my previous knowledge of tables), but I would like to specifically know if they can be used to save functions for properties like character walkspeeds to be used later.
Is it actually possible, and if so, is it really necessary? Or would I be better off maintaining my current practice of inserting them as local functions into server scripts? I just want to know for optimization and workflow purposes, as having self-comprehensible code is important to me, personally.
Also, correct me if this is the wrong category to post in. This is my very first time asking anything on this forum.
You’ll have to be more specific on what you’re trying to accomplish.
Are you wanting to keep track of which players have which walkspeeds at a given time, or are you trying to save persistent Data between servers?
Regardless, you could accomplish this with ModuleScripts if you really wanted to.
Possible? Of course. Necessary? Depends on what you need to do this for.
You do whatever you need to do to stay organized. If using ModuleScripts are going to make your code messy and unreadable, don’t use them. The opposite also holds true.
I want to keep track of their walkspeeds temporarily. My game has a lot of teams that each hold one walkspeed value, and that value is distributed to players via sanity checks upon joining the game. I have machines that require their walkspeed to be 0 and reverted back when they’re done using them.
local module = { }
module.PlayerWalkSpeeds = { }
-- Register the player walk speed, with an optional `currentWalkSpeed` argument if you want give them a custom walk speed.
function module.RegisterPlayerWalkSpeed(player: Player, currentWalkSpeed: number?)
if not currentWalkSpeed then
local Character = player.Character or player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
currentWalkSpeed = Humanoid.WalkSpeed
end
module.PlayerWalkSpeeds[tostring(player.UserId)] = currentWalkSpeed
end
-- Get the walk speed for the specified player, if nil then warn us.
function module.GetPlayerWalkSpeed(player: Player): number?
local PlayerWalkSpeed = module.PlayerWalkSpeeds[tostring(player.UserId)]
if not PlayerWalkSpeed then
warn(`WalkSpeed for player '{player.Name}' does not exist.`)
return nil
end
return PlayerWalkSpeed
end
return module