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

How to find the local player?

script.Parent.Touched:connect(function(hit)
	local h=hit.Parent:findFirstChild("Humanoid")
	if h~=nil then
	game.Players.LocalPlayer.PlayerGui.Minigame.Enabled=true
	end
end)

When I touch the part it says :attempt to index field 'LocalPlayer' (a nil value)

2 answers

You can only use Players.LocalPlayer in a localscript.

Changes to the GUI are probably also best made in a localscript, as the GUI is a local object.

Localscripts do not work in Workspace. Even with FilteringEnabled on however, localscripts can still access the Touched event fired when they touch your block.

So the easiest solution is probably to put this code in a localscript and replace script.Parent (at the start) with the location and name of your object (or make a ObjectValue pointing to it if it's deep inside non-uniquely names structures).

The proper way is in the end to communicate between server and client through RemoteEvents, in this example triggered by the touched event on the server and received by the client, so that you can for example also safely register on the server that the player is now allowed to use this GUI.

Hope this helps. Good luck!

3 comments

0

my answer works im pretty sure right me if im wrong

0

@Benjamin_pro1000 I commented on your answer: It does not for FE, only for experimental mode.

1

thx

try this

script.Parent.Touched:connect(function(hit)
    local h=hit.Parent:findFirstChild("Humanoid")
    if h~=nil then
    game.Players:GetPlayerFromCharacter(h.Parent).PlayerGui.Minigame.Enabled=true
    end
end)

what i did is game.Players:GetPlayerFromCharacter(h.Parent) this searches for a player with that character

3 comments

0

I don’t think you can access GUI by server scripts, and if this is a local script, then the .Touched event won’t work.

0

This does not work with filteringEnabled on. It does work without FilteringEnabled on (so in Experimental Mode), but I don't recommend it! The reason it doesn't work is you have to modify the GUI from a localscript, as @Denny9876 points out. Try it for yourself!

0

@Denny9876 The touched event is however accessible from localscript, even with FilteringEnabled, even between parts that you don't have NetworkOwnership of. But I agree that it's much cleaner/more professional to do in a script.

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