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

Works in play solo, but not online mode?

player = script.Parent.Parent
thrust = game.ReplicatedStorage.BodyThrust
force = game.ReplicatedStorage.BodyForce
f = force:Clone()
f.Parent = player.Character.Head
while true do
	locationX = player.Character.Head.Position.X
locationY = player.Character.Head.Position.Y
locationZ = player.Character.Head.Position.Z
	m = Instance.new("Part")
	m.Shape = "Ball"
	m.Size = Vector3.new(1,1,1)
	m.CanCollide = false
	t = thrust:Clone()
	t.Parent = m
	m.Parent = game.Workspace
	m.BrickColor = BrickColor.new("Brown")
	m.CFrame = CFrame.new(locationX, locationY - 3 , locationZ)
	game:GetService("Debris"):AddItem(m, 5)
	wait(.05)
end

This is my script, the issue is, that it works entirely fine in play solo, but doesn't work at all in online mode. The error I get is: 11:41:29.475 - Players.Player1.PlayerGui.LocalScript:7: attempt to index field 'Character' (a nil value) 11:41:29.475 - Script 'Players.Player1.PlayerGui.LocalScript', Line 7

1 answer

Oftentimes the Player loads in such a way that the LocalScript in question will start running before the actual Workspace representation of the Character has loaded, so common practice when dealing with a LocalScript is to wait() for certain things to exist, like-so:

while not game.Players.LocalPlayer do wait() end
player = game.Players.LocalPlayer
while not player.Character do wait() end
character = player.Character
while not character.Head do wait() end
head = character.head

That way you won't be trying to index a field that doesn't exist yet. It took me a long while to figure out why my LocalScripts were breaking in Online mode, too!

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