Working on module scripts, need a little bit of help

How would I import this with require() to another script?



local anims = {}

anims.id = {8358967032}

return anims

if it helps, the path is game.ServerScriptService.coolmodulescript2

2 Likes

To import a module script all you need to do is call the require function and pass in the path to the module script

local anims = require(game.ServerScriptService.coolmodulescript2)

and to access something from it

--Previous code is up here
local id = anims.id
print(id)

--RESULT

--8358967032

local Module = require(game.ServerscriptService:WaitForChild(“coolmodulescript2”))

Ty!!! i think i’m still indexing wrong though. this is the script i have

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(pl2)
		local anims = require(game.ServerStorage.coolmodulescript2)
		local id = anims.id
        print(id)
		pl2.Animate.run.RunAnim.AnimationId = id
	end)
end)


i also changed the id to a string and include the rbxasset

The issue is that AnimationId must be a string alone, but you are providing it with a string inside of a table. Change the id to anims.id = "rbxassetid://8358967032"

or change your code to

pl2.Animate.run.RunAnim.AnimationId = "rbxassetid://"..id

Your problem now lies that you tried to require the module script from ServerStorage but you said its in ServerScriptService

So it should be (like I said before)

and then

Moved it and it still doesn’t work

Try changing the id to anims.id = "rbxassetid://8358967032

Did that as well, sorry for not responding

Show script now please

need more chars

	plr.CharacterAdded:Connect(function(pl2)
		local anims = require(game.ServerScriptService:WaitForChild("coolmodulescript2"))
		print(anims.id)
		local id = anims.id
		pl2.Animate.run.RunAnim.AnimationId = id
	end)
end)

The problem with this is that you are referencing the array holding the id’s, not the actual Id itself.

Also it’s good coding practice to name your variables in the plural when it’s something like an array then name each independently accessed variable in the singular.

	plr.CharacterAdded:Connect(function(pl2)
		local anims = require(game.ServerScriptService:WaitForChild("coolmodulescript2"))
		print(anims.id)
		local id = anims.id
		pl2.Animate.run.RunAnim.AnimationId = id
	end)
end)

Here you go

If you only need one value in the module script then you can just do this:

plr.CharacterAdded:Connect(function(pl2)
		pl2.Animate.run.RunAnim.AnimationId = 8358967032
	end)
end)

There’s no reason to use a module script here.

Practicing using module scripts, and yes, i know

1 Like

Well, if that’s the case then it might be better to set your module up as a dictionary.

local animationCollection = {}

animationCollection.RunAnim = 8358967032
animationCollection.JumpAnim = --example animation id

return 

This way you could have a folder with different modules that contain information to different animations.

I didnt even notice the error until you said that.

The problem here is

You wrapped the value in {} instead of just putting the value.

So the thing you should do is:

anims.id = 8358967032

Or if you leave it like that in the retrieving script you would need to do:

local anims = require(game.ServerScriptService:WaitForChild("coolmodulescript2"))
local id = anims.id[1]
1 Like
-- What's wrong with doing it this way?
local AnimationModule = {
    ["Run Animation"] = {
        AnimationId = 1234567890
    },

    ["Etc"] = {
        AnimationId = 1122334455
    }
}

return AnimationModule

Not to mention you can do this after

local PlayerService = game:GetService("Players")
local AnimationModule = require(script.AnimationModule) -- replace this with the spot of the animation module

PlayerService.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
        -- Now set the animation. If the player is walking, the animation won't change automatically due to how Roblox set their script up
        Character:WaitForChild("Animate").run.RunAnim.AnimationId = "rbxassetid://"..AnimationModule["Run Animation"].AnimationId
    end)
end)

-- sorry I changed the "plr", and "p12", because I strongly dislike the use of those variable names in new work

If you’re trying to make it so the running animation changes in the middle of the current walking animation, you cannot do that because Roblox made their script in a very strange way that resets the Running animation after the player stopped moving. There is a community tutorial available that explains how you could get past that;