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??
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.
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?
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)
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
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.