ModuleScript attempting to index nil value

I’ve pretty much ignored module scripts until now, because I never thought I’d need them. My module script prints this error: 20:41:49.148 - ReplicatedStorage.ModuleScripts.KillModule:2: attempt to index a nil value.
This is the script.

local func = function(Character, Room)
	Character:MoveTo(game.Workspace:FindFirstChild(Room).Spawn)
end
return func

It’s a tiny script, so I probably just have no idea how module scripts work. The hub wasn’t helpful at all Tbh.

character.Humanoid:MoveTo(location)

1 Like

Try: character.Humanoid:MoveTo(location) – its solution

Here is what you would put in the script:

local func = function(Character, Room)
    if Character and Room then
       Character:MoveTo(workspace:FindFirstChild(Room).Spawn.Position)
   end
end

I’ve always used :MoveTo on the Character’s model, and it’s worked.

Yeah, I actually just realized there is another method for instances, where it moves the entire model to a location. Wasn’t thinking.

In that case, you need to pass in the actual Position property of the BasePart you want the player to move to, not the BasePart directly:

local func = function(Character, Room)
	Character:MoveTo(game.Workspace:FindFirstChild(Room).Spawn.Position)
end
return func

I just realized that and fixed it, but it still didn’t work

What is ‘Room’?

Is it a string (text) or a model?

Hard to know exactly what’s going on as I don’t know where you’re calling this function from and what parameters you’re passing through.

Well, here’s one of the scripts that require it.

script.Parent.Touched:connect(function(hit)
if game.Players:GetPlayerFromCharacter(hit.Parent) then
local killFunction = require(game.ReplicatedStorage.ModuleScripts.KillModule) -- The module script
killFunction(hit.Parent, game.Workspace.Room2) -- Room 2 is a model
end
end)

Okay yeah, so the method FindFirstChild takes a string as an argument, but you’re passing in an instance to be the ‘Room’ argument, which is why you’re getting an error.

Simple fix:

local func = function(Character, Room)
	Character:MoveTo(Room.Spawn.Position)
end
return func
1 Like

Ooooh, facepalm. Thanks!
(30 chars)

1 Like