LocalScript Impacting Other Clients?

A LocalScript in StarterPlayerScripts which shows a GUI to the player when they touch a part. Simple. Except, someone joined my game and THEIR local script was making the GUI appear on MY client’s screen also? This doesn’t make any sense with my knowledge of how LocalScripts are supposed to work??

This is my script inside a LocalScript in StarterPlayerScripts:

local GUI= script.Parent.Parent.PlayerGui:WaitForChild(“GUI”).CoreGUI
game.Workspace.Sell.Touched:Connect(function(hit)
–make GUI visible
end)

I have have not tried putting it inside StarterGui or something, but I’m wondering now: HOW is a LOCAL SCRIPT inside a LOCAL PLAYER changing other players’ GUI??

2 Likes

This is because game.Workspace.Sell.Touched fires when anything touches it, not just when a player touches it,which is why everyone’s guis pops up, for that you would have to implement a few checks

local Players = game:GetService("Players")
local lplr = Players.LocalPlayer

game.Workspace.Sell.Touched:Connect(function(hit)
   local plr = Players:GetPlayerFromCharacter(hit.Parent) 
   if plr and plr == lplr then 
      --make gui visible
   end 
end)

Essentially, you check whether the part touched a player, and if that player is the local player, before opening a gui.

7 Likes

OH i see. that’s potentially a LOT of firing for .touched then. Is this still the best way to show a GUI? Or is that many fires not that big of a deal anyway?

It probably is the best way. Unless you want to use GetTouchingPart Region3 or Raycast

You should check if the GUI is shown already to save function calls

1 Like

Thats more of an opinionated question, as the difference between say that, or using a remote event would be negligible (someone correct me if im wrong about that)

At that point, you would be getting into the territory of micro-optimization, which might not be worth the effort

(I would like to make clear, im not advocating the use of REs in this instance, i believe a client sided touched event would be best here)

Using remote events is usually not a better solution as it can create breaches and potential exploits.

I’m talking about the .touched event firing still for every single touch still, it is repeatedly checking the same conditions more than necessary

What I would do is use RunService and check for the magnitude. You could also use Region3 as @theking48989987 mentioned

Probably a Region3 would be the most efficient way to do it but performance wise it’s such a negligible difference it’s barely noticeable.

1 Like

yeah i wouldn’t use a remote event, that would just create unnecessary latency

I’m not advocating for the use of remote events here, still, i dont really see how a purely Server → Client remote can create breaches and exploits

1 Like

Sorry I misunderstood I thought it was the opposite. (Client → Server)

You could do this on a LocalScript(Region3 method):

local Offset = .1

game:GetService("RunService").Heartbeat:Connect(function()

	local pos = HumanoidRootPart.CFrame.Position

	local min = Vector3.new(pos.X + Offset,pos.Y + Offset, pos.Z + Offset)
	local max = Vector3.new(pos.X - Offset,pos.Y - Offset, pos.Z - Offset)
	
	local region = Region3.new(max, min)
	region = region:ExpandToGrid(4)
  

for _,Part in pairs(game.Workspace:FindPartsInRegion3(region,nil,math.huge)) do
  if Part.Name == "DisplayGuiPart" then 
   --Display()
  else
  --DisableDisplay()
  end
end

end

RenderStepped seems to be firing 50 times per second. Interesting solution, but it makes Touched look like the most optimal solution here.

I guess they both have their pros and cons

Using a RemoteEvent would prevent unnecessary overhead (traffic). You shouldn’t have a need to do that when you can connect the event from the client. If touch isn’t important and it’s just being used to show a Gui, the client can handle that without relying on the server and it can be done quickly.


@KhanPython Realistically you shouldn’t be using RenderStepped for this. RenderStepped can block frames from rendering until all its code is ran and you aren’t rendering anything here. I’d change to Stepped or Heartbeat instead.

1 Like