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

How do you fix attempt to index field 'LocalPlayer' a nil value?

Here's my script;

local part = script.Parent
local activated = false

part.Touched:connect(function()
	if activated == false then
		activated = true
		print("activated is now true")
		local chance = math.random(1,100)
		print(chance)
		if chance > 1 and chance < 40 then
			print("Found something")
			game.Players.LocalPlayer.Character:WaitForChild("Humanoid").WalkSpeed = 0
			game.Players.LocalPlayer.PlayerGui.BattleGuiTest:WaitForChild("IntroTop"):TweenPosition(UDim2.new(0, 0,0, 0),'Out','Bounce',3.5)
			game.Players.LocalPlayer.PlayerGui.BattleGuiTest:WaitForChild("IntroBottom"):TweenPosition(UDim2.new(0, 0,0, 200),'Out','Bounce',3.5)
			wait(5)
			game.Players.LocalPlayer.PlayerGui.BattleGuiTest:WaitForChild("BattleBackGround").Visible = true
			wait(0.5)
			--Might add functions
			game.Players.LocalPlayer.PlayerGui.BattleGuiTest:WaitForChild("IntroTop"):TweenPosition(UDim2.new(0, 0,-1, 0),'In','Bounce',3.5)
			game.Players.LocalPlayer.PlayerGui.BattleGuiTest:WaitForChild("IntroBottom"):TweenPosition(UDim2.new(0, 0,0, 800),'In','Bounce',3.5)
		else
			print("found nothing")
			print("chance")
			if activated == true then
				return
			end
		end
	end
end)

Some notes: -This error only appears in multiplayer (not studio)

I can not post output, please help!

If you wish to see the error yourself then press F9 while playing this game; https://web.roblox.com/games/734875822/Future-Game-Leak

1 answer

✓ Accepted answer

Server scripts cannot access LocalPlayer.

Use a LocalScript instead, with RemoteEvents if necessary.

2 comments

0

oh, there's more

0

There is. He was only asking about LocalPlayer being nil.

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