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

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

Asked by 6 years ago
Edited 6 years ago

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

Log in to vote
2
Answered by
Dog2puppy 168
6 years ago
Edited 6 years ago

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.

0
Touched event shouldn't be handled on the client. Also, LocalScripts wouldn't run under OP's part. User#19524 175 — 6y
0
"treats the player and client as the same thing" lol they ARE the same thing User#19524 175 — 6y
0
Oh yeah. I always forget about the LocalScript restrictions on where they'll run. :P Dog2puppy 168 — 6y
Ad
Log in to vote
1
Answered by 6 years ago

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)

Answer this question