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

Help with remote event?

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?

1 answer

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)

3 comments

0

What can I do to fix the design problems you listed?

0

it also seems that my script had a line missing, how can I include it inside the script? (line 08)

0

Ah, I thought I was missing somthing. My code fixed the majority of the issues, but it'll take another segment to activate the billboard again.

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