Is it possible to destroy old rays?

I am very nervously asking this question because it might be impossible but i am trying to destroy old rays that are not being used anymore. I have issues with the rays slowing down the game because there is so much of them. Is there any possible way to do this?

1 Like

By “rays”, do you mean the ones made with Ray.new? If so, the moment you’re done using the ray (that is, the scope that set the variable ends), it’s garbage collected, which basically means it’s destroyed and removed from memory. As an example:

Ray.new(...) -- A ray is created but then immediately garbage collected, since you can't access it anyways
local ray = Ray.new(...) -- This ray is not garbage collected yet because the variable has been declared
print(ray)
-- Now, if there's nothing else that'll be using this variable, the ray is garbage collected.
2 Likes

yes I am using Ray.new in a while wait() loop (which is probably unreliable), does it still get garbage collected in a loop? cause ever since i started using this method the game has been slowing down around the 2 minute mark

1 Like

You need to post your code so that the specific implementation can be reviewed.

1 Like

As long as the Ray is declared inside that while loop and nowhere else (not a global, added to a table, etc), yes, it will be garbage collected. If you’re adding the ray to a table, then you need to remove the ray from the table before it can be garbage collected. If not, I doubt this is the cause of your performance issues.

1 Like
local found = false
while wait() do
	if game.Players.LocalPlayer.OtherValues.XboxEnabled.Value == true then
		script.Parent.TextButton.ImageLabel.Image = "rbxasset://textures/ui/settings/help/YButtonDark.png"
		script.Parent.TextButton.Text = ""
	elseif game:GetService("UserInputService").TouchEnabled == true then
		script.Parent.TextButton.ImageLabel.Size = UDim2.new(1, 0, 1, 0)
		script.Parent.TextButton.Text = "Tap"
		script.Parent.TextButton.ImageLabel.Image = "rbxassetid://1594360148"
	else
		script.Parent.TextButton.ImageLabel.Size = UDim2.new(1, 0, 1, 0)
		script.Parent.TextButton.Text = "E"
		script.Parent.TextButton.ImageLabel.Image = "rbxassetid://1594360148"
	end
	for _,box in pairs(game.Workspace.EPositions:GetChildren()) do
		found = false
		local IgnoreList = {game.Workspace.Bowling.TrashCans, game.Workspace.Bowling.LeftRooms.SecurityOffice.Door, game.Workspace.Bowling.ClickRADIO, game.Workspace.Bowling.JanitorCloset, game.Workspace.Bowling.ArcadeRoom.Games, box}
		local ray = Ray.new(box.Position, (game.Players.LocalPlayer.Character.HumanoidRootPart.Position-box.Position).unit * 10)
		local part, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, IgnoreList, false, true)
		if part then
			if part:FindFirstAncestor(game.Players.LocalPlayer.Name) then
				found = true
			end
		end
		if found == true then
			script.Parent.TextButton.TextLabel.Text = box.Name
			script.Parent.Enabled = true
			script.Parent.Adornee = box
			break
		else
			script.Parent.Enabled = false
			script.Parent.Adornee = nil
		end
	end
end

Rays are not real instances, they don’t have a :Destroy() method.

Though I can’t really see anything that would cause any outstanding performance issues, I’d recommend you move IgnoreList out of the while loop if it will never change.

3 Likes

ill try it and see what happens

it is significantly slower since the start at the 3 minute mark, so i dont think the table was the issue

1 Like

How many things are in game.Workspace.EPositions:GetChildren()
It could be possible that you are just raycasting way too much, since this is all in a wait() loop.

8 parts are in the folder, so idk

1 Like

As I said before, the raycasting definitely isn’t the issue and nothing in the code you posted should cause your game to slow down. Have you tried testing your game on a different device to see whether it’s just an issue with your device?

usually slows down on my phone (its a 6 something) and i asked other people and they said it was happening for them too, doesn’t work on the computer though (for me)

1 Like

Have you tried checking the developer console to see whether the devices are running low on memory, or if a script is using too much CPU time? You should be able to see it on mobile by typing /console in chat and checking the Memory, Scripts, and ServerJobs tabs. In Scripts and ServerJobs, look for anything listed as being greater than 3% activity.

For a more advanced way to debug your performance issues see this article.

2 Likes

nothing is above 3 in the scripts activity but the rate is going up really high, with me not knowing much about using this side of the console, is that supposed to happen?

and the microprofiler cpu time rises over time

1 Like