Hello there, im a new developer and not very good, im currently working on a game and i need some help as i cant find any way to make what i want to do.
So, i have a script that allows players to press a button which then shows a ghost object of the object they chose when they hover with their mouse over a surface, The Problem is that theres no grid-system
Which means that u can place the object freely anywhere on the surface and even inside eachother
also by grid system i mean something like this
But that it appears on the surface when you press one of the buttons and disappears when u finished the placement:
it would also be nice if it would be so that u cant place the objects inside eachother
so, i have 2 codes, one is the placement and the other one is the script for the buttons and ghost objects to show
heres the script for the buttons and all that:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")
local player = Players.LocalPlayer
local camera = game.Workspace.Camera
local mouse = player:GetMouse()
local playergui = player:WaitForChild("PlayerGui")
local plopScreen = playergui:WaitForChild("ScreenGui")
local plopChairButton = plopScreen:WaitForChild("PlopChairButton")
local plopTableButton = plopScreen:WaitForChild("PlopTableButton")
local raycastParameters = RaycastParams.new()
raycastParameters.FilterType = Enum.RaycastFilterType.Whitelist
raycastParameters.FilterDescendantsInstances = { game.Workspace.Surfaces }
local ghostObjects = ReplicatedStorage:WaitForChild("GhostObjects")
local ghostChair = ghostObjects:WaitForChild("GhostChairTest")
local ghostTable = ghostObjects:WaitForChild("GhostTable")
local events = ReplicatedStorage:WaitForChild("Events")
local plopEvent = events:WaitForChild("PlopEvent")
local plopCFrame = nil
local activeGhostObject = nil
local rotationAngle = 0
local PLOP_CLICK = "PLOP_CLICK"
local PLOP_ROTATE = "PLOP_ROTATE"
local PLOP_MODE = "PLOP_MODE"
local RAYCAST_DISTANCE = 200
local ROTATION_STEP = 90
local function onRenderStepped()
local mouseRay = camera:ScreenPointToRay(mouse.X, mouse.Y, 0)
local raycastResults = game.Workspace:Raycast(mouseRay.Origin,
mouseRay.Direction * RAYCAST_DISTANCE, raycastParameters)
if raycastResults then
local rotationAngleRads = math.rad(rotationAngle)
local rotationCFrame = CFrame.Angles(0, rotationAngleRads, 0)
plopCFrame = CFrame.new(raycastResults.Position) * rotationCFrame
activeGhostObject:SetPrimaryPartCFrame(plopCFrame)
activeGhostObject.Parent = game.Workspace
else
plopCFrame = nil
activeGhostObject.Parent = ReplicatedStorage
end
end
local function onMouseInput(actionName, inputState)
if inputState == Enum.UserInputState.End then
activeGhostObject.Parent = ReplicatedStorage
RunService:UnbindFromRenderStep(PLOP_MODE)
ContextActionService:UnbindAction(PLOP_CLICK)
ContextActionService:UnbindAction(PLOP_ROTATE)
plopChairButton.Visible = true
plopTableButton.Visible = true
rotationAngle = 0
if plopCFrame and activeGhostObject then
plopEvent:FireServer(plopCFrame, activeGhostObject.Name)
end
end
end
local function onRotate(actionName, inputState)
if inputState == Enum.UserInputState.End then
rotationAngle += ROTATION_STEP
if rotationAngle >= 360 then
rotationAngle = 0
end
end
end
local function onPlopButtonActivated(activeGhostObj)
plopChairButton.Visible = false
plopTableButton.Visible = false
RunService:BindToRenderStep(PLOP_MODE,
Enum.RenderPriority.Camera.Value + 1, onRenderStepped)
ContextActionService:BindAction(PLOP_CLICK, function(actionName, inputState)
onMouseInput(actionName, inputState)
end, false, Enum.UserInputType.MouseButton1)
ContextActionService:BindAction(PLOP_ROTATE, onRotate, false, Enum.KeyCode.R)
end
local function onPlopChairButtonActivated()
activeGhostObject = ghostChair
onPlopButtonActivated(activeGhostObject)
end
local function onPlopTableButtonActivated()
activeGhostObject = ghostTable
onPlopButtonActivated(activeGhostObject)
end
plopChairButton.Activated:Connect(onPlopChairButtonActivated)
plopTableButton.Activated:Connect(onPlopTableButtonActivated)
and heres the script for the placement, however i dont think that u will need to edit it, but if u have to u can do it:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local events = ReplicatedStorage:WaitForChild("Events")
local plopEvent = events:WaitForChild("PlopEvent")
local ploppables = ServerStorage:WaitForChild("Ploppables")
local chair = ploppables:WaitForChild("ChairTest")
local table = ploppables:WaitForChild("Table")
local function onPlop(player, cframe, objectType)
local objectCopy
if objectType == "GhostChairTest" then
objectCopy = chair:Clone()
elseif objectType == "GhostTable" then
objectCopy = table:Clone()
else
warn("Invalid objectType received.")
return
end
objectCopy:SetPrimaryPartCFrame(cframe)
objectCopy.Parent = game.Workspace
end
plopEvent.OnServerEvent:Connect(onPlop)
Thanks to everyone that will try to help me, and if anything is in bad grammar or isn’t written correctly, then im sorry, my english isnt that good.