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

I have a 1 part that when touched is supposed to make a Gui appear, but it wont? Help?

script.Parent.Touched:connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") then game.Players.LocalPlayer.PlayerGui.Shop.Frame.Visible = true end end)

The Script is inside the part. I know it is not a problem with the gui because i tested it without the part prior.

It works in studio but not in game.

My error I am getting is

Workspace.ShopPart.Script:3: attempt to index field 'LocalPlayer' (a nil value)

i only get it when i test in the start thing.

2 answers

You are using a Script it appears and not a LocalScript. Experimental Mode was recently removed, and the server can no longer interact with player guis (but this still wouldn't work with experimental mode). The reason this works in Studio is because Play Solo treats the player and client as the same thing.

To fix your issue, you just need to use a LocalScript.

EDIT: Ignore this now, incapaz wrote a better answer, and I forgot to mention LocalScript restrictions.

3 comments

0

Touched event shouldn't be handled on the client. Also, LocalScripts wouldn't run under OP's part.

0

"treats the player and client as the same thing" lol they ARE the same thing

0

Oh yeah. I always forget about the LocalScript restrictions on where they'll run. :P

###This is because LocalPlayer is nil on the server. You can only use it in LocalScripts. Use a RemoteEvent to fire to the client to open the gui. Place a RemoteEvent and name it something like "OpenShopGui".

-- LocalScript under your frame

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local OpenGui = ReplicatedStorage:WaitForChild("OpenShopGui")

OpenGui.OnClientEvent:Connect(function()
    script.Parent.Visible = true
end)

###Server code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local OpenGui = ReplicatedStorage:WaitForChild("OpenShopGui")

script.Parent.Touched:Connect(function(part) -- Switch to :Connect, :connect is deprecated
    local plr = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)

    if plr then
        OpenGui:FireClient(plr)
    end
end)

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