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

The Gui doesnt show when a part is touched?

Asked by 5 years ago

script.Parent.Touched:Connect(function(hit) print "Door Touched" if hit.Parent:FindFirstChild("Humanoid") then game.StarterGui.DoorGui.Frame.Visible = true print "Gui Shown" end end)

For some reason, when a player comes in contact to the Door that is there, and its touched the frame doesn't show although its visibility is true

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Basically the problem is that you are using the StarterGui.

StarterGui

This service, the content of it will get cloned locally into each player's PlayerGui. Say you have some GUIs in there, they all get cloned into the player's PlayerGui.


PlayerGui

The LocalScripts in this folder are the ones running and the GUIs the player sees and the ones they interact with are the ones all in PlayerGui. If you edit something in StarterGui the edit will be applied once the player resets.


So what was happening?

You weren't editing the player's PlayerGui GUI.

How do we fix this?

There are a few ways.

1. RemoteEvent

You would insert a RemoteEvent (preferably in ReplicatedStorage) and a localscript (preferably inside your frame)

The local script would be this:

local remoteEvent = game:GetService("ReplicatedStorage").RemoteEvent

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

In here it's listening for the OnClientEvent event of remote event. When it is fired by the server the code in the function will execute

And on the server side

local Players = game:GetService("Players")
local remoteEvent = game:GetService("ReplicatedStorage").RemoteEvent

script.Parent.Touched:Connect(function(part)
    local client = Players:GetPlayerFromCharacter(part.Parent)

    if client then
        remoteEvent:FireClient(client)
    end
end)

Here it's just firing the remote event to the client who touches the part.

2. Handle the touched event locally

I personally recommend this one, there will be that immediate feedback and you don't need the server doing any firing to the client

LocalScript, preferably in your frame

local part = game:GetService("Workspace").Part -- whatever your part is
local client = game:GetService("Players").LocalPlayer

part.Touched:Connect(function(other)
    script.Parent.Visible = other:IsDescendantOf(client.Character)
end)

object:IsDescendant(ancestor) returns true if object is a descendant of ancestor, and when a player touched something it's a body part of their character; the leg, hand, ect.

So this is just assigning the Visible property to the return value of the function.

It is important that this is done because Touched can fire for anything touching it, another player, even a non player! So this is just to guarantee the local player touched it.

0
Thanks for the help. RebornedInFire 35 — 5y
Ad
Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

The reason for this is that you are modifying object in the StarterGui instead of in a Player's PlayerGui.


The Difference


Everything inside the StarterGui is cloned in its current state to a player's PlayerGui when they join a game, so therefore, modifying the StarterGui would have no impact of that player's guis.


Quick Fix


local plr = game.Player.LocalPlayer
workspace.Thing.Touched:Connect(function(hit)
    if hit:IsDescendantOf(plr.Character) then
        plr.PlayerGui.DoorGui.Frame.Visible = true
    end
end)

Note that the PlayerGui is a child of the player object, and that I checked whether if the object that was hit was a descendant of the player's character, in order to prevent it from running when any other part or npc touched the part

Hopefuly this helped!

0
That's not gonna work; the contents of StarterGui are cloned locally User#24403 69 — 5y
1
oh, yeah, local scripts can still listen to touched events though. theking48989987 2147 — 5y

Answer this question