I’m using the Touched event, and it seems a bit laggy. Is there a better solution than using this event? Like raycasting or something?
Using connections to the .Touched event in itself is not inherently laggy. It’s quite possible your code is lacking debounces or anything similar. To get better suggestions it’s a good idea to post your code so we can see what could be causing the lag you experience.
local module = {}
-----
-----
local Object = _G.GetObject("Agni", "Fireball")
local Speed = _G.MaxSpeed
local Damage = 20
local Cooldown = 0.5
-----
-----
function CreateFireball(Origin, Hit) -- CFrame; CFrame
local Fireball = Object:Clone()
Fireball.CFrame = CFrame.new(Origin.p, Hit.p)
local Force = _G.CreateForce.Velocity(Origin, Hit, Speed)
Force.Parent = Fireball
_G.TagObject(Fireball)
Fireball.Parent = _G.Container
return Fireball
end
function Touched(obj, Fireball, Player) -- Object it touched; Fireball; Player who shot fireball
print("HI")
if (not obj) or (not obj.Parent) then return end
if (obj.Parent == Player.Character) or (obj.Parent.Parent == Player.Character) then return end
_G.FindHumanoidAndDamage(obj, Player, Damage)
_G.TweenObject(Fireball, 0.5, {Size = Vector3.new(5,5,5), Transparency = 1})
Fireball.Anchored = true
Fireball.CFrame = obj.CFrame
return true
end
-----
module.Run = function(Player, Hit, ...) -- Player who shot; Where they clicked in world; anything else
local Char = Player.Character or Player.CharacterAdded:Wait()
local Hroot = Char:FindFirstChild("HumanoidRootPart")
local Fireball = CreateFireball(Hroot.CFrame, Hit)
Fireball:SetNetworkOwner(Player)
local TouchOn = true
local Debounce = false
Fireball.Touched:Connect(function(obj)
if not TouchOn then return end
if (obj.Parent == Char) or (obj.Parent.Parent) == Char or (obj:FindFirstChild("IsAMagic")) then
return
else
if Debounce then return end; Debounce = true
local doReturn = Touched(obj, Fireball, Player)
if doReturn then TouchOn = false end
wait(Cooldown); Debounce = false
end
end)
_G.DebrisItem(Fireball, 3)
end
-----
-----
return module
sorry it’s kind of messy
All of these calls to arbitrary methods in the global table is a bit odd and definitely slower than keeping these methods on the stack via local variables. It also makes it very difficult to tell what actually happens with each .Touched connection. From what I can see so far, I think you’d be better of using :Disconnect on the event connection (store the returned rbxeventconnection in a variable to call this on) to avoid the event firing more than necessary. Again with all the _G methods within your Touched function I find it hard to say anything meaningful about what causes the lag.
Disconnect actually seemed to help significantly. Thanks
Also, sorry about the globals, I just need to use the same functions for many different scripts so I thought it would be useful.
Glad that that made a difference. As for using globals; that is not per se bad, but storing the indexed function in a local variable puts the function on the stack for much faster access.
local aMethod = _G.aMethod
aMethod()
as opposed to
_G.aMethod()
each time it is called
also if the topic is considered solved to you, use the ‘solved’ checkmark on the bottom of the post that provides the solution to mark it such.
I’ll be sure to do that, thanks for taking the time to help me!