Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to find the local player?

Asked by 6 years ago
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

Log in to vote
1
Answered by
Nonaz_jr 439 Moderation Voter
6 years ago

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!

0
my answer works im pretty sure right me if im wrong Benjamin_pro1000 3 — 6y
0
@Benjamin_pro1000 I commented on your answer: It does not for FE, only for experimental mode. Nonaz_jr 439 — 6y
1
thx 0_Halloween 166 — 6y
Ad
Log in to vote
0
Answered by 6 years ago

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

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. User#20279 0 — 6y
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! Nonaz_jr 439 — 6y
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. Nonaz_jr 439 — 6y

Answer this question