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

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

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

✓ Accepted answer

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

5 comments

0

Although this solution is valid, this can probably be handled completed from the client, see my answer :)

0

I appreciate the fast response! I completly forgot about using remote events.

0

Very true. I also believe that it's important to give the person as many options as possible. :)

0

yes, but i believe this solution is inferior due to the extra networking and latency that it will cause

0

if something can be done on the client without any negative effects, do it on the client

###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.

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