I have this inside a localscript in startergui
local p = game.Players.LocalPlayer local m = p:GetMouse() local s = game.Workspace:WaitForChild("select") for _, v in pairs(s:GetChildren()) do m.Move:connect(function(gui) if m.Target == v and p:DistanceFromCharacter(v.Position)< 50 then v.BillboardGui.Enabled = true m.Button1Down:connect(function(a) game.Workspace.Mouse:FireServer() end) else v.BillboardGui.Enabled = false end end) end
and this inside a serverside script for the remote event
local event = Instance.new("RemoteEvent") -- creates the event object event.Parent = game.Workspace -- puts the event into the Workspace event.Name = "Mouse" -- giving a name to the event so we can fire it specifically elsewhere event.OnServerEvent:connect(function(player, part) -- define the function that will be called when the event is fired print(part.Name) end)
How can I make it so it prints the name of the part located inside the "select" model which was clicked by the player?
When you do the FireServer()
, you need to pass in the actual part touched. In this case, v
.
There's also another major issue with your client-side code; by connecting the events inside of loops, you are creating an excessive number of event connections.
Additionally, the Button1Down event gets connected regardless of if they just clicked or not, so an excessive amount of fireserver calls will be made, regardless of if the user was clicking on a specific part or not, or none will be created at all.
Either way, bad design.
local p = game.Players.LocalPlayer local m = p:GetMouse() local s = game.Workspace:WaitForChild("select") m.Button1Down:connect(function() if m.Target.Parent == s and p:DistanceFromCharacter(m.Target.Position) < 50 then game.Workspace.Mouse:FireServer(m.Target) end end) m.Move:connect(function() for _, v in pairs(s:GetChildren()) do if v ~= m.Target then v.BillboardGui.Enabled = false else v.BillboardGui.Enabled = true end end end)