LocalPlayer can't be found

Hello, its ExoticTwix. I am trying to make a screen gui enable on touch from player gui.But whats happening is it cant find local player. Please help me image

You’re attempting to access game.Players.LocalPlayer from a server script. The server doesn’t know which player you want, since it is the server, not the client.

Try using Players:GetPlayerFromCharacter() to accomplish this!

3 Likes

You can use,

script.Parent.Touched:Connect(function(hit)
if game.Players:FindFirstChild(hit.Parent.Name) then
game.Players:WaitForChild(hit.Parent.Name).PlayerGui.ScreenGui.Enabled = true
end
end)

Since you are using game.Players.LocalPlayer with a server script, it will not work since only the client can use that and the server script is only for the server. I agree with @Deferend since :GetPlayerFromCharacter also works as well.

This doesn’t make sense. If your if statement evaluates to true, that means that the Player instance already exists in the Players service. Therefore, there is no reason to wait for it to exist… since it exists.
Additionally, there is a built-in method to retrieve the Player instance given the player’s character: Players | Documentation - Roblox Creator Hub

Hmm… I guess you’re right. I don’t really need to wait for it. Well, thanks for that, but it atleast still works! Your script does too!

1 Like

first of all you cant use local player inside normal script and you should try use this:

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		player:FindFirstChild("PlayerGui").ScreenGui.Enabled = true
	end
end)
3 Likes

Don’t use a server script to enable Guis. If the client is capable of disabling a Gui, the server will continue to recognise it as Enabled and any subsequent attempts to set the Gui’s Enabled property to true will not re-enable the Gui. You shouldn’t be using the server to work with Guis in the first place.

Things only happen when a change is detected. The server will see that it’s attempting to set something to enabled when it already is, so no change will actually occur. This’ll work the first time and every time afterward, it will not.

3 Likes

I tryed it and it works Thanks alot for helping :slight_smile:

Is there a way so it happens more than once?

what you mean? like if you hide the ui so you want make the ui visible again?
if yes so is will do that too. :wink: (just check if you hide the frame or the screen) by the way you can also use Remote Event for that.

What’s the point of calling FindFirstChild if you’re not checking whether it’s nil or not?

Like right now when you touch it enables then you click the exit gui and it un enables. But the problem is i want it so the gui gets enabled again when you touch the part but for some reason it only works once

Read Colbert2677’s post, it would be better to enable gui from the client instead of the server. It’s not working or wont work how you intended it to because the server doesn’t know that the gui has been disabled via clicking the exit button. I would recommend as mentioned using a RemoteEvent and fire the client instead with it

Something like this:

script.Parent.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	  if player then
   RemoteEvent:FireClient(player) 
	end
end)

Client:

local player = game.players.LocalPlayer


RemoteEvent.OnClientEvent:Connect(function()
local PlayerGui = player.PlayerGui
   local screengui = PlayerGui:FindFirstChild("ScreenGui") or PlayerGui:WaitforChild("ScreenGui")
  screengui.Enabled = not screengui.Enabled
end)
2 Likes