Many ClickDetectors without Lag

I need a ClickDetector for many objects in a game, if I were going to put a script in every ClickDetector in every object, I feel like there would be lag issues, is there a way to do this more efficiently to prevent lag or would it only cause minimal lag?

1 Like

Raycast from the client from the mouse to whitelist to your Soda folder, then fire an event to the server

Okay, I will try that, thank you!

1 Like

Instead of using a clickDetector, I’d make a custom one that just checks when the mouse is clicked and if they are clicking on a soda

https://create.roblox.com/docs/reference/engine/classes/UserInputService

https://create.roblox.com/docs/reference/engine/classes/Mouse


local UIS = game:getService("UserInputService")

UIS.InputBegan:Connect(function(Input)
	if Input.UserInputType == Enum.UserInputType.MouseButton1 then
local Mouse = Player:GetMouse()
if Mouse.Hit.Target.Name == "Soda" then
-- Run Script
end
end
end)

Above is just an example, it most likely wont work just by copy and pasting it into a script.

1 Like

Listen to @StraightScared’s message if u don’t want people to click through anything like other players or walls btw

1 Like

Try using a module and requiring it for each and every instance that is in that folder. (You will thank me later. Editing one script will make you realize to change everything else and you will have to suffer the pain of copy and paste). I recommend modules for the sake of being neat but you do you.

Example:

SodaModule

local sodaFunctions = {}

function sodaFunctions.DefaultSodaOnClick(soda)
	-- do the code
end

return sodaFunctions

Server:

local sodaModule = require(script.SodaModule)
for i,  v in pairs (SodaFolder:GetChildren()) do
	sodaModule.DefaultSodaOnClick(v)
end

Another benefit of using modules is it can make it easier for you to make varients (Let’s say you want a soda doing something a bit different you could add it to the module and distinguish it through the loop.

1 Like

Just use a single script to loop through each one and connect them to events which perform the action you are trying to do.

1 Like

Thank you for helping me out, it works!

1 Like

You can put a clickdetector in a model and it will work for every part in it

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.