"Attempt to index nil with character" When used on the command line

I printed the character of the player I assigned the script to, and it didn’t error. Yet when I use the script it errors.


Magic.Player was defined as game.Players.gameowner101
image
This is the function.

This only happens when I use the command line, everything works just as intended when I use anything else to use the function and fill in the variables.

you could get the character directly, by workspace[username]

That would force me to change up the script and then change it back to normal.

In the image you sent, the Magic.Player is set to nil.

Yeah, it gets defined by the command on the command line you can see it int he first picture.

Blast.Player = game.Players.gameowner101

your character doesnt exist while you arent playing the game

are you running this during playtesting?

Well can you try this:

Magic.Player = game.Players:GetPlayerFromCharacter(workspace["gameowner101"])

Yeah, I am running it during playtesting.

try printing Player at the start of the spells function

It is in fact, printing nil… how would I fix that?

well if player is printing nil then you probably arent defining the variable in the metable correctly

how does the script work normally?

In any situation in which I am defining the player as long as it’s not the command line it works perfectly fine.

The module works by first of all, creating a player variable which is equal to nil, whenever the module runs, a script defines the player as the player that is using it, then runs every command with that player’s name.

That’s what i actually stated to you above, have you tried m script?

well, yeah, but its still defining the player explicitly so it should still work fine

The issue here is that when you set Magic.Player, the local variable Player has already been set on require.

it will probably work, but they want Magic.Player to be defined properly during playtesting

1 Like

Same error,

local Blast = require(game.ServerStorage.MagicModule) Blast.Player = game.Players:GetPlayerFromCharacter(workspace.gameowner101) Blast.Spells.Blast()

arent you overwriting it with the redefinition?

It is best practice not to create local variables for stuff like this and instead just use Magic.Player in place of Player.

Can you put your user in this format [“idk”]