ClickDetector Unable To Use ModuleScript

Hey! In short whenever I click a ClickDetector I want to make a Lightning happen there. However it doesn’t want to activate for some reason if I’m using it with a ClickDetector. I put it in a normal script and it ran perfectly fine there. I have no idea what’s the problem here so help would be appreciated!

local Model = script.Parent
local ClickDetector = Model.ClickDetector

local Click = function()
	local Module = require(game.ReplicatedStorage.LightningModule)
	for i = 1,100,1 do
		wait()
		Module.Bolt(game.Workspace.Start.Position,script.Parent.Top.Position,12,3,.3)
	end
end

ClickDetector.MouseClick:Connect(Click)
3 Likes

Hello!
I tried to replicate this with a ModuleScript and for me it seemed to work just fine.

Could you possibly elaborate? Any errors? It may be a problem with Roblox, but just asking!
Thanks!

I guess there is something wrong with the ModuleScript. If there had been any errors I would’ve already said them. I’ll paste the ModuleScript as well. I can’t really see anything wrong with it.

local Lightning = {}

local TweenS = game:GetService("TweenService")

function drawLightning(p1,p2,parent,size)
	
	local part = Instance.new("Part",parent)
	part.Anchored = true
	part.Material = Enum.Material.Neon
	part.BrickColor = BrickColor.new("Deep orange")
	
	local dist = (p2 - p1).Magnitude
	
	part.Size = Vector3.new(size,size,dist)
	part.CFrame = CFrame.new(p1,p2) * CFrame.new(0,0,-dist/2)
	
	TweenS:Create(part,TweenInfo.new(.15),{Size = Vector3.new(0,0,dist)}):Play()
	delay(.2,function()
		part:Destroy()
	end)
end

Lightning.Bolt = function(p1,p2,NumOfPoints,offset,size)
	
	local Points = {}
	
	for i = 0, NumOfPoints do
		
		local Offset = Vector3.new(math.random(-offset,offset),0,math.random(-offset,offset))
		
		if i == 0 or i == NumOfPoints then
			Offset = Vector3.new(0,0,0)
		end
		
		local part = Instance.new("Part",workspace.Camera)
		part.Anchored = true
		part.Material = Enum.Material.Neon
		part.BrickColor = BrickColor.new("Deep orange")
		part.Size = Vector3.new(.5,.5,.5)
		part.Transparency = 1
		
		part.Position = p1 + (p2 - p1).Unit * i * (p2 - p1).Magnitude/NumOfPoints + Offset
		
		table.insert(Points,part.Position)
		
	end
	
	for i, p in pairs(Points) do
		if Points[i+1] ~= nil then
			drawLightning(Points[i],Points[i+1],workspace.Camera,size)
		end
	end
	
end


return Lightning

Did you try adding print()'s in the code to check what executes?
This might be a problem with Roblox.

Yeah it prints it out. As you are saying I think it is a problem with roblox. Do you know any alternate solutions?

You can Mouse.Target which returns the part which the mouse is touching, or nil if the mouse is pointed towards the skybox. Using that, you can make a system with the help of an local script, server script and a remote event. Something like this:
image

Inside of the Local script you can write something like this:

local player = game:GetService("Players").LocalPlayer

local func = game.ReplicatedStorage.ClickedWithTarget

local mouse = player:GetMouse()

mouse.Button1Down:Connect(function() 
	func:InvokeServer(mouse.Target)	
end)

and for the service script:

local whatToCheckOnClicked = workspace.CLICKME -- the part to check if clicked

game.ReplicatedStorage.ClickedWithTarget.OnServerInvoke = function(player, param)
	if (param ~= nil) and (param == whatToCheckOnClicked) then
		-- param holds the clicked part
		-- do something
	end
end

You can also use the remote event later if you want to check if the player clicked.
Hope this helps! Ask me if i have to explain anything.

1 Like

Yeah that works now. Thank you so much for you help! You are a really kind person! <3

1 Like

No problem!
Just realized that the local userInputService = game:GetService("UserInputService") is not needed. There might be some simpler way of doing this, but for some reason a lot of things you can do in only specific situations.

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