Clicking a part not working

I’m trying to make some sort of system where there (when activated) is a box around each player in the game apart from the localplayer, when this box is made it’s supposed to be able to be clicked by the localplayer and the name of that player would be put into the localplayer’s gui.

The thing is that when i’m clicking the part that’s on the player(s) character it doesnt print anything or work at all. I need some help with this.

Clicking Script: (Server)

script.Parent.MouseClick:Connect(function(plr)
	print("Clicked")
	local selectingPlayer = game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent.Parent)
	game.ReplicatedStorage.MCI.Remotes:WaitForChild("Select"):FireClient(plr, selectingPlayer)
end)

Local Script:

local gui = script.Parent
local plr = game.Players.LocalPlayer

local UserInputService = game:GetService("UserInputService")

local MainFrame = script.Parent.MainFrame
local TopBar = MainFrame:WaitForChild("TopBar")

local Camera = workspace:WaitForChild("Camera")

local DragMousePosition
local FramePosition

local Draggable = false

TopBar.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
		Draggable = true
		DragMousePosition = Vector2.new(input.Position.X, input.Position.Y)
		FramePosition = Vector2.new(MainFrame.Position.X.Scale, MainFrame.Position.Y.Scale)
	end
end)

TopBar.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton1 or input.UserInputType == Enum.UserInputType.Touch then
		Draggable = false
	end
end)

UserInputService.InputChanged:Connect(function(input)
	if Draggable == true then
		local NewPosition = FramePosition + ((Vector2.new(input.Position.X, input.Position.Y) - DragMousePosition) / Camera.ViewportSize)
		MainFrame.Position = UDim2.new(NewPosition.X, 0, NewPosition.Y, 0)
	end
end)

MainFrame.SelectionButton.MouseButton1Click:Connect(function()
	if MainFrame.SelectTxt.Text == "Select" then
		MainFrame.SelectTxt.TextColor3 = Color3.fromRGB(255, 175, 62)
		MainFrame.SelectTxt.Text = "Selecting"
		for i,v in pairs(game.Players:GetPlayers()) do
			if v.Name ~= plr.Name then
				local partThing = game:GetService("ReplicatedStorage").MCI.Model.MCI:Clone()
				local weld = Instance.new("WeldConstraint")
				weld.Parent = v.Character
				weld.Part0 = partThing
				weld.Part1 = v.Character.HumanoidRootPart
				partThing.Position = v.Character.HumanoidRootPart.Position
				partThing.Orientation = Vector3.new(v.Character.HumanoidRootPart.Orientation.X, v.Character.HumanoidRootPart.Orientation.Y  - 90, v.Character.HumanoidRootPart.Orientation.Z)
				partThing.CP.Position = v.Character.HumanoidRootPart.Position
				partThing.CP.Orientation = Vector3.new(v.Character.HumanoidRootPart.Orientation.X, v.Character.HumanoidRootPart.Orientation.Y  - 90, v.Character.HumanoidRootPart.Orientation.Z)
				partThing.Parent = v.Character
			end
		end
	else
		MainFrame.SelectTxt.TextColor3 = Color3.fromRGB(81, 109, 39)
		MainFrame.SelectTxt.Text = "Select"
		for i,v in pairs(game.Players:GetPlayers()) do
			if v.Name ~= plr.Name then
				v.Character.MCI:Destroy()
			end
		end
	end
end)

game.ReplicatedStorage.MCI.Remotes:WaitForChild("Select").OnClientEvent:Connect(function(plr, selected)
	print("Ran function")
	MainFrame.PlayerName.Text = selected.Name
	MainFrame.SelectTxt.TextColor3 = Color3.fromRGB(109, 51, 51)
	MainFrame.SelectTxt.Text = "Unselect"
	for i,v in pairs(game.Players:GetPlayers()) do
		if v.Name ~= plr.Name then
			v.Character.MCI:Destroy()
		end
	end
end)

The box’s initial setup:
image

Could anyone please help me resolve this issue? Thank you.

Hi! now I know that I’m not that much of a remote event kind of guy, but is .MCI for the clicking script a remote event, or is it just Remote inside it?

It’s a folder that has the remote in it. I’m trying to troubleshoot a solution. So my current status is that I get the part working and that it selects the player. But it only works once.

You might have forgot to use OnClientEvent for your clicking script. Try this:

game.ReplicatedStorage.MCI.Remote.Select.OnClientEvent:Connect(function()
	script.Parent.MouseClick:Connect(function(plr)
		print("Clicked")
		local selectingPlayer = {}
        game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent.Parent)
	    game.ReplicatedStorage.MCI.Remotes:WaitForChild("Select"):FireClient(plr, selectingPlayer)
	end)
end)

This is where i’m at right now. It doesnt let me unselect and select any other players than the first one. So the box dissapears but doesnt change the text as seen in the video

Maybe use a random call:

game.ReplicatedStorage.MCI.Remote.Select.OnClientEvent:Connect(function()
	script.Parent.MouseClick:Connect(function(plr)
		print("Clicked")
		local selectingPlayer = math.random(1, #game.Players)
        game.Players:GetPlayerFromCharacter(script.Parent.Parent.Parent.Parent)
	    game.ReplicatedStorage.MCI.Remotes:WaitForChild("Select"):FireClient(plr, selectingPlayer)
	end)
end)

I’m not selecting a random person. I want the one you click on basically.

Don’t worry, is it ok if I try what you did in studio and try to see what the problem is?

Can’t rn sorry. Going to bed lol

Ok then, sorry I couldn’t fix it.

No problem man! Have an amazing day!

1 Like

You might find this function helpful:

local function GetUnselectedPlayers(selectedPlayer)
	local PlayerList = game.Players:GetPlayers()
	for k,v in pairs(PlayerList) do
		if v.Name == selectedPlayer.Name then
			table.remove(PlayerList, k)
			break
		end
	end
	return PlayerList
end

local SelectedPlayer = -- this is the player that is selected.
local UnselectedPlayers = GetUnselectedPlayers(SelectedPlayer)
for _,player in pairs(UnselectedPlayers) do
	-- Use 'player' to put click box on them.
end
1 Like
table.remove(Array, table.find(Array, Element))

actually I meant to use table.remove(PlayerList, k) as k would be the index # but yea that would work too.

I was focusing on removing the loop.