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)
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()
end
end)
IMPLEMENTATION
local function loadSelectionFunctions()
createSelectable(workspace:WaitForChild('Part1'), true)
createSelectable(workspace:WaitForChild('Part2'), true)
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!
2 comments
press f9 in roblox game output to find any errors or yields
There is none, it works in studio