You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve? Efficient code
-
What is the issue? Im not sure if my code leaks, Because it does go up when in use, But after a while it goes down(My old game is a linear rising forever), But it is still net positive, And yes, Disconnect connection, But some cant be disconnected, And most of the time, My scripts are destroyed when not in use (A script inside a player character being deleted once the player leave eg)
Some code
Placement
local contextactionservice = game.ContextActionService
--Modules
local VisualIndicator = require(game.ReplicatedStorage.Modules["Placement Transparency"])
local Tween = require(game.ReplicatedStorage.Modules.Tweening)
--Stuff
local currentindex = 1
local gridsize=8
local objects = game.ReplicatedStorage.Part:GetChildren()
local state = 0
table.sort(objects,function(a,b)
return a.Id.Value < b.Id.Value
end)
local currentobject = nil
local function grid(x,y,z)
local x2 = (math.floor(x/gridsize)*gridsize)+gridsize/2
local z2 = (math.floor(z/gridsize)*gridsize)+gridsize/2
return Vector3.new(x2,y,z2)
end
local mouse = game.Players.LocalPlayer:GetMouse()
local currentPivot
function MainLoop()
spawn(function()
local plot = workspace["Player_"..game.Players.LocalPlayer.UserId].PlacementArea
local plotBase = workspace["Player_"..game.Players.LocalPlayer.UserId].Plot
local param = RaycastParams.new()
--param.FilterType = Enum.RaycastFilterType.Whitelist
--param.FilterDescendantsInstances = {plot}
while currentobject ~= nil do
game["Run Service"].RenderStepped:Wait()
if currentobject ~= nil then
--Where the mouse hit *in world- space*
local result = workspace:Raycast(workspace.CurrentCamera.CFrame.Position,(mouse.UnitRay.Direction)*200,param)
if result then
local mouseHit = CFrame.new(result.Position)
--Where the mouse hit *in the Plots object-space* (i.e. relative to the part)
local mouseHitRelative = plotBase.CFrame:ToObjectSpace(mouseHit)
--Where the mouse hit, snapped *in the Plots object-space*
local mouseHitRelativeSnapped = CFrame.new(grid(mouseHitRelative.X,0,mouseHitRelative.Z))
--Where the mouse hit *in world- space*, snapped *in the Plots object-space*
local mouseHitWorldSnapped = plotBase.CFrame * mouseHitRelativeSnapped
currentPivot = mouseHitWorldSnapped
pcall(function()
if currentobject.Type.Value == "Path" then
Tween.tweenModel(currentobject,mouseHitWorldSnapped)
else
Tween.tweenModel(currentobject,mouseHitWorldSnapped)
end
end)
end
end
end
end)
end
local function handleTab(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then
print("Handled")
state = state + 1
if state == 1 then
currentobject = objects[currentindex]:Clone()
currentobject.Parent = workspace
MainLoop()
VisualIndicator.makeTransparent(currentobject)
elseif state == 2 then
game.ReplicatedStorage.Remotes.ClientToServer.Placement.Place:FireServer(currentPivot,currentindex)
state = 0
if currentobject ~= nil then
currentobject:Destroy()
currentobject = nil
end
end
end
end
local function handleE(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then
currentindex = math.clamp(currentindex+1,1,#objects)
if currentobject ~= nil then
currentobject:Destroy()
currentobject = objects[currentindex]:Clone()
currentobject.Parent = workspace
VisualIndicator.makeTransparent(currentobject)
end
end
end
local function handleQ(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then
currentindex = math.clamp(currentindex-1,1,#objects)
if currentobject ~= nil then
currentobject:Destroy()
currentobject = objects[currentindex]:Clone()
currentobject.Parent = workspace
VisualIndicator.makeTransparent(currentobject)
end
end
end
contextactionservice:BindAction("Place",handleTab,true,Enum.KeyCode.C)
contextactionservice:BindAction("E",handleE,true,Enum.KeyCode.E)
contextactionservice:BindAction("Q",handleQ,true,Enum.KeyCode.Q)
A* connection generation
local module = require(script.GetPart)
local Neighbors = require(script.FindNeighbors)
local basepath = script.Parent.Path
local nodes = {}
basepath.ChildAdded:Connect(function(child)
child.Name = "Node"
local NewObjPos = script.Parent.Plot.CFrame:ToObjectSpace(child.CFrame).Position
local x,z = NewObjPos.X,NewObjPos.Z
if nodes[tostring(math.round(NewObjPos.X))] == nil then
nodes[tostring(math.round(NewObjPos.X))] = {}
nodes[tostring(math.round(NewObjPos.X))][tostring(math.round(NewObjPos.Z))] = child
else
nodes[tostring(math.round(NewObjPos.X))][tostring(math.round(NewObjPos.Z))] = child
end
--print(nodes)
local tilesize = 8
local startick = tick()
local xplus = tostring(math.round(tonumber(x)+tilesize))
local xneg = tostring(math.round(tonumber(x)-tilesize))
local zplus = tostring(math.round(tonumber(z)+tilesize))
local zneg = tostring(math.round(tonumber(z)-tilesize))
local XP,XN,ZP,ZN = nil,nil,nil,nil
local success,er = pcall(function()
XP = Neighbors.XComp(xplus,z,nodes)
XN = Neighbors.XComp(xneg,z,nodes)
ZP = Neighbors.XComp(x,zplus,nodes)
ZN = Neighbors.XComp(x,zneg,nodes)
end)
if not success then error(er) return end
print(ZP,ZN,XP,XN)
for _,Neighbor in pairs({XP,XN,ZP,ZN}) do --Creating connection
if Neighbor then
local FIELD_1 = Instance.new("ObjectValue") --local field
local FIELD_2 = Instance.new("ObjectValue") --neighbors field
print("FOUND NEIGHBOR")
FIELD_1.Value = Neighbor
FIELD_2.Value = child
FIELD_1.Parent = child
FIELD_2.Parent = Neighbor
end
end
end)
Tweening
local module = {}
local tweenService = game:GetService("TweenService")
local info = TweenInfo.new(0.5)
function module.tweenModel(model, CF)
local CFrameValue = Instance.new("CFrameValue")
CFrameValue.Value = model:GetPivot()
CFrameValue:GetPropertyChangedSignal("Value"):connect(function()
model:PivotTo(CFrameValue.Value)
end)
local tween = tweenService:Create(CFrameValue, info, {Value = CF})
tween:Play()
tween.Completed:connect(function()
CFrameValue:Destroy()
end)
end
return module
What I mainly want from this post is what can be improved, To make my game efficient, Memory wise and to improve my coding style to allow for that change.