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

How to stop a GUI showing up for everyone on server?

Asked by 5 years ago

I made a gui that is in starterGui and has nothing, but a frame in it. The gui pops up on touched, but the problem is, is that it's showing up on everyone elses screen. The code is in a local script inside of the screenGui. I'm stuck on what to do.

partTest = game.Workspace.TestTOuchy
gui = script.Parent

partTest.Touched:Connect(function(hit)
    if (hit.Parent:FindFirstChild("Humanoid")~= nil) then

    script.Parent.Enabled = true
        wait(2)
        script.Parent.Enabled = false   
    end
end)

2 answers

Log in to vote
0
Answered by 5 years ago

Explanation

This is due to how you just detect if a part was touched. This would result in all of the LocalScripts detecting that it was touched even if it was another player.

The Solution

An easy fix for this is to create a RemoteEvent in ReplicatedStorage so that the part can send the open gui request to specific players. Below is the code that would help:

Script in the Part:

local guiEvent = Instance.new("RemoteEvent", game.ReplicatedStorage)
guiEvent.Name = "guiEvent"

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        guiEvent:FireClient(player)
    end
end)

LocalScript in the Gui:

local guiEvent = game.ReplicatedStorage:WaitForChild("guiEvent")

guiEvent.OnClientEvent:Connect(function()
    script.Parent.Enabled = true
    wait(2)
    script.Parent.Enabled = false
end)

That should fix your problem!

If you have any other questions, just reply to this answer! :)

-Crystal

0
Although this solution is valid, this can probably be handled completed from the client, see my answer :) BenSBk 781 — 5y
0
I appreciate the fast response! I completly forgot about using remote events. MCrafter49 8 — 5y
0
Very true. I also believe that it's important to give the person as many options as possible. :) Crystalflxme 104 — 5y
0
yes, but i believe this solution is inferior due to the extra networking and latency that it will cause BenSBk 781 — 5y
0
if something can be done on the client without any negative effects, do it on the client BenSBk 781 — 5y
Ad
Log in to vote
0
Answered by
BenSBk 781 Moderation Voter
5 years ago
Edited 5 years ago

Problem

When partTest is touched by any Player's character, the body of the if statement will execute. I assume this is not your desired result, as you want the body to execute only if the local Player touches partTest.

Solution

We can use Players:GetPlayerFromCharacter to check if the character that touched partTest belongs to the local Player. If this isn't the case, do not enable the GUI.

Nitpicks

  • You should be using local variables instead of global variables, as the former is more performant and, in many cases, functionally superior.

Code

local players = game:GetService("Players")
local local_player = players.LocalPlayer
local partTest = workspace.TestTOuchy
local gui = script.Parent

partTest.Touched:Connect(function(hit)
    -- Check if the Player (or nil) returned from the function is not the local Player.
    if players:GetPlayerFromCharacter(hit.Parent) ~= local_player then
        return
    end
    gui.Enabled = true
    wait(2)
    gui.Enabled = false
end)
If this helped, remember to accept the answer for others who come across the same issue.

Answer this question