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

Why LocalPlayer doesn't work in game but it works in studio?

I have tried many ways like using local script but still doesn't work so I try to use normal script again. For the local script the LocalPlayer works but my other script doesn't work, it stops. Maybe it's because I put all of the script together. I'm confused about the local script and LocalPlayer. The script is for minigame that I am making. So here's my script :

--start
game.Players.ChildAdded:wait()
--variables
Map1 = game.ServerStorage.Maps.Arena1
NotificationGui1 = game.Workspace.StatusBar.Gui1.TextBox.Text
NotificationGui2 = game.Workspace.StatusBar.Gui1.TextBox.Text
local Player1 = game.Players.LocalPlayer
Winner = "None"
WinnerFound = false
enabled = true
Reciver = game.Workspace.Spawn.Spawn1

--Reciver Vars
Spawn1o = 0
Spawn2o = 0
Spawn3o = 0
Spawn4o = 0
Spawn5o = 0
Spawn6o = 0

function onTouched(part)
	local hum = part.Parent:FindFirstChild("Humanoid")
	if hum~=nil then 
		if enabled == true then 
			enabled = false
			local t = part.Parent:FindFirstChild("Torso")
			local t2 = Reciver
			t.CFrame = CFrame.new(t2.Position.x, t2.Position.y+4, t2.Position.z)
			Spawn1o = Spawn1o +1
			wait(1)
			enabled = true
			if Spawn1o == 4 then
				Reciver = game.Workspace.Spawn.Spawn2
				if Spawn2o == 4 then
				Reciver = game.Workspace.Spawn.Spawn3
				    if Spawn3o == 4 then
				    Reciver = game.Workspace.Spawn.Spawn4
				        if Spawn4o == 4 then
				        Reciver = game.Workspace.Spawn.Spawn5
				            if Spawn5o == 4 then
				            Reciver = game.Workspace.Spawn.Spawn6
				            end
				        end
				    end
				end
			end
		end
	end
end

game.Workspace.WinnerDetector.Touched:connect(function(Player)
	WinnerFound = true
	Winner = Player.Parent.Parent.Name
	Player.Parent.Humanoid.Health = 0
end)
while true do
NotificationGui1 = "Please Wait"
NotificationGui2 = "Please Wait"
print("Wait60")
wait(60)
NotificationGui1 = "Current Map :", Map1.MapName
NotificationGui2 = "Current Map :", Map1.MapName
print("LoadMap")
wait(5)
NotificationGui1 = "Loading..."
NotificationGui2 = "Loading..."
wait(2)
Map1:clone().Parent = game.Workspace
Player1.PlayerGui.WinnerGui.TextBox.Text = "Teleporting Players"
game.Workspace.TpSender.Touched:connect(onTouched)
wait(24)
NotificationGui1 = "Game In Progress"
NotificationGui2 = "Game In Progress"
game.Players.LocalPlayer.PlayerGui.WinnerGui.TextBox.Text = "Game In Progress"
if WinnerFound then
WinnerFound = false
Player1.PlayerGui.WinnerGui.TextBox.Text = "Winner : ", Winner
game.Workspace.Map:destroy()
Spawn1o = 0
Spawn2o = 0
Spawn3o = 0
Spawn4o = 0
Spawn5o = 0
Spawn6o = 0
NotificationGui1 = "Game Ended"
NotificationGui2 = "Game Ended"
wait(10)
Player1.PlayerGui.WinnerGui.TextBox.Text = "Winner : Not Found"

end
end

4 comments

0

because local player is local hos, thus your computer is not some else's local host, if you want the code to work either place it in starter gui, starter tools, playerscripts,

0

on a side note the only real difference from using local script vs script is organization of code handling, and knowing that you placed the right code in the right place,because calling local player simply will be null unless it is in one of the catagories that i have previously mentioned, also it allows the user not to see code through a 3rd party application, in terms of difference vs local scri

0

Code in a ServerScript runs strictly on the server you are connected to, where as code in a LocalScript will run strictly on the client/player's computer. That is the difference between the two.

0

@blackJPI I explained that more simple

2 answers

✓ Accepted answer

If it's in a LocalScript:

You're waiting for a Player to join. Unfortunately, when you join that event will not fire for LocalScripts listening to it. That in turn means that unless another player joins the game, the LocalScript is never going to get past line 1.


If it's in a Script

You can't use Players.LocalPlayer in a Script, because it simply doesn't know anything about it on the server. You can get the first Player to join if you replace your first line with

local Player1 = game.Players.PlayerAdded:wait()

Which will assign Player1 to the first Player to join the server. Be aware that this may or may not play nice with how you expect your game to work, and how or where you do stuff is down to you.

In studio, it makes everything run through the server (studio wise). In-game, you must use a local script when connecting to a local player and do not use localscripts for anything other than the client's children. This script would not work with multiple users if you're attempting to use it in a local script and same with a server script.

Local Reason: It'd run on all clients and be different for everyone causing your game to look very glitchy and buggy because instead of running one time, it'll be running several times through several clients.

Server Reason: You cannot connect to a local player through server scripts, if you're attempting to grab players I recommend you use

for i,v in pairs(game.Players:GetChildren()) do
print(v.Name) -- v represents the player.
end

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