Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why is character not a valid member of player?

For some reason, my ModuleScript is erroring because of the player.

Server Script:

for i, player in pairs(game.Players:GetPlayers()) do
	table.insert(players, player)
	print(player.Name)
end

mainModule:StartPlayers(players)

And my module script code:

function module.StartPlayers(players)
	local spawnPoints = map:WaitForChild("SpawnPoints")
	local spawns = spawnPoints:GetChildren()
	
	wait(5)
	
	for i, player in pairs(players) do
		if player then
			
			character = player.Character
			
			if character then
				
				--Morph
				
				local chosenMorph = game.ServerStorage.Tornado
				
				for _, v in pairs(character:GetChildren()) do
					if v:IsA("BasePart") then
						v.Transparency = 1
					elseif v:IsA("Accessory") then
						v:Destroy()
					end
				end
				
				local clonedMorph = chosenMorph:Clone() --Creating morph
				clonedMorph.Parent = character
				clonedMorph.Name = player.Name.."_Tornado"
				
				local morphWeld = Instance.new("Weld")--Welding morph to player
				morphWeld.Parent = clonedMorph --Use tornado part or whatever here!
				morphWeld.Part1 = clonedMorph --Same here!
				morphWeld.Part0 = character:FindFirstChild("HumanoidRootPart")
				
				character.Humanoid.WalkSpeed = chosenMorph.Speed.Value
				character.Humanoid.JumpPower = 0
				
				--Teleport
				
				character:FindFirstChild("HumanoidRootPart").CFrame = spawns[1].CFrame + Vector3.new(0,12,0)
				table.remove(spawns, 1)
			end
		end
	end
end

It errors at character = player.Character Error: "Character is not a valid member of Model"

Oddly enough, making it character = workspace:FindFirstChild(player.Name) fixes that error, but then it errors at 'character.Humanoid.WalkSpeed = chosenMorph.Speed.Value' saying that "Humanoid is not a valid member of Model"

How do I fix this? Also, is there a way to have, sort of a global table for all of the players that any script can access? That would make coding easier.

1 answer

✓ Accepted answer

A couple of things you need to watch out for here:

  1. You’re calling the method as mainmodule:StartPlayers(players) which is syntactic sugar for calling the StartPlayers function like this: mainmodule.StartPlayers(mainModule, players). You can fix this by either calling as mainmodule.StartPlayers(players) or defining as function mainModule:StartPlayers(players)

  2. You seem to running this as soon as the server begins, which is a bad idea since it would work only for the first player (maybe for the first). Instead you should use the Players.PlayerAdded event to initiate each player.

  3. Extending the second point, you’ll have to remove the loop from the ModuleScript and handle each player individually. Only one player in one scope.

  4. If you want to share data between two scripts, bindable events/functions are generally the way to go. Though it’s validity depends on whether you want the client to be able to receive data from the server as well.

2 comments

0

Well, I didn't include the full server script, it waits before running all of this. Also, on your third point, what do you mean by that? I only have about 5 months of experience XD

1

Also, using a colon seemed to fix the issue.

This discussion is preserved as it was. New answers, comments, and votes are closed.