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

ServerScriptService.Lessons:5: attempt to index field 'LocalPlayer' (a nil value)?

while true do wait(5)
	lessons=math.random(1,5)
	if lessons == 1 then
		--swimming
		game:WaitForChild("ServerStorage").Lessons:clone().Parent = game.Players.LocalPlayer.PlayerGui
		game:WaitForChild("Players").LocalPlayer.PlayerGui.Lessons.Swimming.Visible = true
		wait(60)	
	end
end

and i get the error ServerScriptService.Lessons:5: attempt to index field 'LocalPlayer' (a nil value) how did i fix it? Edit now geting this Infinitie yield possible on "Game:WaitForChild"

4 comments

0

normal script

0

You can not use LocalPlayer in a Server Script. You can only use LocalPlayer in a LocalScript.

0

helped alot

0

but still not working

1 answer

✓ Accepted answer

Problem

LocalPlayer doesn't exist in ServerScripts, but ServerStorage doesn't exist in LocalScripts.

Solution

Use the PlayerAdded function to find the player

Final Script

Note: This is a Server Script

game:GetService("Players").PlayerAdded:connect(function(player)
	while true do
		wait(5)
		lessons=math.random(1,5)
			if lessons == 1 then
			--swimming
			game:WaitForChild("ServerStorage").Lessons:clone().Parent = player.PlayerGui
			player.PlayerGuiLessons.Swimming.Visible = true
		wait(60)    
		end
	end
end)

1 comment

0

thx bro ill see if it works

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