Cone Placement System For Driving Game

Here I made a pretty basic cone placer so I can test some driving mechanics and learn a bit more about scripting. It uses one server script for spawning the cones and three local scripts for Gui and mouse detection.

I think this system is pretty bulky and slow and can take a lot of improvement. If anyone has suggestions, please share them.

Also, how could I make a transparent cone follow the mouse’s position when the mode is set to on? Sort of like a tower defense game shows the preplacement.

This is the server-side script

local replicatedStorage = game:GetService("ReplicatedStorage")
local positionFinderEvent = replicatedStorage:WaitForChild("Remotes").PositionFinder
local conesFolder = game.Workspace:WaitForChild("Cones")
local coneTemplate = replicatedStorage:WaitForChild("Cone")
local coneMode = replicatedStorage:WaitForChild("Remotes").ConeMode
local coneClear = replicatedStorage:WaitForChild("Remotes").ConeClear
local coneModeBool

coneMode.OnServerEvent:Connect(function (player, newConeModeBool)
    print(player, "Has set cone mode to", newConeModeBool)
    coneModeBool = newConeModeBool
end)

positionFinderEvent.OnServerEvent:Connect(function(player, mousePosition)
    if coneModeBool == true then
        local newCone = coneTemplate:Clone()
        newCone.Position = mousePosition + Vector3.new(0,2,0)
        newCone.Parent = conesFolder
        print("Cone Created")
    else
        print("Cone Mode is off")
    end
end)

coneClear.OnServerEvent:Connect(function()
	for i,v in pairs(conesFolder:GetChildren()) do
		v:Destroy()
		print("Cones cleared")
	end
end)

This is the local mouse position finder

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local replicatedStorage = game:GetService("ReplicatedStorage")
local positionFinderEvent = replicatedStorage:WaitForChild("Remotes").PositionFinder

mouse.Button1Down:Connect(function()
    local mousePosition = mouse.Hit.p
    positionFinderEvent:FireServer(mousePosition)
end)

And here is the cone mode on/off, and the clearer.

local coneModeButton = script.Parent
local coneModeRemote = game:GetService("ReplicatedStorage"):WaitForChild("Remotes").ConeMode
local visual = coneModeButton:WaitForChild("Frame")
local coneMode = false  

coneModeButton.Activated:Connect(function()
    coneMode = not coneMode  
    coneModeRemote:FireServer(coneMode)
	if coneMode then
		visual.BackgroundColor3 = Color3.fromRGB(47, 255, 0)
        print("Client CM on")
	else
		visual.BackgroundColor3 = Color3.fromRGB(255, 0, 4)
        print("Client CM off")
    end
end)

local clearConesButton = script.Parent
local coneFolder = game.Workspace:WaitForChild("Cones")
local coneClear = game:GetService("ReplicatedStorage"):WaitForChild("Remotes").ConeClear

clearConesButton.Activated:Connect(function()
	coneClear:FireServer()
end)
1 Like

for the transparent cone. you should use a event inside Run Service called RenderStepped, connect a function and re-position the cone to your mouse (just make sure to not fire the event so it doesnt make a new cone on the server-side every frame lol)

you should also disconnect the function whenever the tool is unequipped. here is an example on how for the disconnect part.

local connection

Tool.Equipped:Connect(function(mouse)
	-- create a ghost cone
	
	connection = game["Run Service"].RenderStepped:Connect(function()
		-- you do your ghost cone re-position here
	end)
end)

Tool.Unequipped:Connect(function()
	connection:Disconnect()
	-- Disconnect the function to stop it (recudes resource usage when not trying to place cone)
	-- you should also delete the ghost cone here
end)