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

Mouse Click Function Works In Studio, Not In Game..?

I have tested this multiple times. The mouse hover, and leave script functions normally, however the actual click does not.

script.Parent.ClickDetector.MouseHoverEnter:connect(function(Clicker) 
		if script.Parent:FindFirstChild("SelectionBox") then
			print("googoo")
		else
	
local selection = Instance.new("SelectionBox")
	selection.Color3 = Color3.new(153, 153, 153)
	selection.Parent = Clicker.PlayerGui
	wait()
	selection.Adornee = script.Parent
	wait()
	end
end)

script.Parent.ClickDetector.MouseHoverLeave:connect(function(Clicker)
	if Clicker.PlayerGui:FindFirstChild("SelectionBox") then
		Clicker.PlayerGui.SelectionBox:Destroy()
	wait()
	end
end)

script.Parent.ClickDetector.MouseClick:connect(function(Clicker)
	local key = Clicker:WaitForChild("Key")
	if key.Value == false then
		key.Value = true
		wait(0.1)
		Clicker.PlayerGui.SelectionBox:Destroy()
		wait()
		script.Parent:Destroy()
		wait()
		end
end)


2 comments

0

press f9 in roblox game output to find any errors or yields

0

There is none, it works in studio

1 answer

Hi SooGloezNoob,

I made something similar to this yesterday for a game of my own. Below are some possible issues that may be causing your script to break, and also some tips that I suggest you use to help improve efficiency.

Make sure the code is in a LocalScript, and not a normal script. Use :Connect instead, as :connect is now deprecated. Not sure why there are so many random wait() statements there, they aren't necessary. You have scripted your program so that the selection box is destroyed not only when the mouse hovers over, but also when the click event fires.

A more efficient way of doing something like this would be to create clickDetector and selectionBox instances in the replicatedStorage, and then cloning these instances whenever you need them instead of creating new ones. I've stored these objects in a folder called replications in the RS.

CLIENT

local replications = replicatedStorage:WaitForChild('Replications')
local clickDetector = replications:WaitForChild('ClickDetector')
local selectionBox = replications:WaitForChild('SelectionBox')

local objectClickEvent = replicatedStorage:WaitForChild('Object_Click_Event')

local function createSelectable(object, isClickable)
	local clickDetectorClone = clickDetector:Clone()
	clickDetectorClone.Parent = object
	local selectionBoxClone = selectionBox:Clone()
	selectionBoxClone.Parent = object
	selectionBoxClone.Adornee = object

	clickDetectorClone.MouseHoverEnter:Connect(function()
		selectionBoxClone.Transparency = 0
	end)

	clickDetectorClone.MouseHoverLeave:Connect(function()
   		selectionBoxClone.Transparency = 1
	end)
	
	if isClickable then
		clickDetectorClone.MouseClick:Connect(function()
			objectClickEvent:FireServer('obj_click', object) -- I strongly recommend the use of FE for events that will translate onto the server to improve the security of your game!
		end)
	end
end

The click event is a Remote Event that is fired when the object is clicked. I use request to ensure that the event is only being fired when the object has been clicked. In this case, data is the object being clicked, which is then destroyed. Its probably safe that you store Remote Events in replicatedStorage.

SERVER

local clickDetector = Instance.new('ClickDetector', replications)
clickDetector.MaxActivationDistance = 20

local selectionBox = Instance.new('SelectionBox', replications)
selectionBox.Color3 = Color3.new(255, 255, 255)
selectionBox.LineThickness = 0.02
selectionBox.Transparency = 1

objectClickEvent.OnServerEvent:Connect(function(player, request, data)
	if request == 'obj_click' then
		data:Destroy() -- Insert your own event here
	end
end)

IMPLEMENTATION

local function loadSelectionFunctions()
	createSelectable(workspace:WaitForChild('Part1'), true)
	createSelectable(workspace:WaitForChild('Part2'), true)
	-- ...Other selectable parts
	-- If you have a model with all the parts you want to become selectable, then you can create a for loop that will load the functions for each child.
end

loadSelectionFunctions()

If you want to learn more about Remote Events and Remote Functions, I suggest this video. However if you choose not to use FE then ensure that the FE option is switched off in the properties of workspace.

The script I've made can be improved a little with debounces on the clicking events. I strongly suggest that you add this in. Here's the wiki page.

Have a wonderful day/night!

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