I am trying to make a building system for a game. Here’s what I have so far (I’ll make the thing that you hold smaller in the future just not at the moment):
There’s a problem though. Those are supposed to be cubes. Not Slabs. See what happens when I make the baseplate transparent?
There’s also a strange glitch that when you try and place it on a platform that’s higher than you are, it flahses and can’t decide where it wants to go.
I’ve tried adding a 1 or 2 to the y position but then it just does that same glitch on any surface.
By the way, because I hate life and I suck at CFrames, rays etc I just used mouse.hit for this. I couldn’t just use mouse.Hit.Position.Y for this either because then the preview starts moving towards your camera uncontrollably. I’ll include my code below for the server and the client.
Client (Inside the tool)
local mouse = game.Players.LocalPlayer:GetMouse()
local RenderStepFunction = nil
local rs = game:GetService("RunService")
script.Parent.Equipped:Connect(function()
local preview = script.Parent.Handle:Clone()
preview.Parent = workspace
preview.Name = "Preview"
preview.Transparency = 0.5
preview.CanCollide = false
preview.Anchored = true
RenderStepFunction = rs.RenderStepped:Connect(function()
if mouse.Target ~= nil and mouse.Target:IsA("BasePart") then
preview.Position = Vector3.new(math.ceil(mouse.Hit.Position.X), mouse.Target.Position.Y * 2, math.ceil(mouse.Hit.Position.Z))
preview.Orientation = Vector3.new(0, 0, 0)
end
end)
end)
script.Parent.Unequipped:Connect(function()
pcall(function()
RenderStepFunction:Disconnect() -- Disconnect the function that makes the preview update
game.Workspace:FindFirstChild("Preview"):Destroy() -- Get rid of the preview
end)
end)
script.Parent.Activated:Connect(function()
if game.Workspace:FindFirstChild("Preview") then
game.ReplicatedStorage.PlaceBlock:FireServer(script.Parent.Name, Vector3.new(4, 4, 4), script.Parent.Handle.Color, game.Workspace.Preview.Position, Vector3.new(0, 0, 0), true, true, game.Workspace.Builds[game.Players.LocalPlayer.Name]) -- Tell the server to make the part.
if script.Parent.Limited.Value == true then
script.Parent:Destroy()
game.Workspace.Preview:Destroy()
end
end
end)
Server side
game.Players.PlayerAdded:Connect(function(player)
local buildsFolder = Instance.new("Folder", workspace.Builds) -- Create a folder for the player's builds
buildsFolder.Name = player.Name
end)
game.Players.PlayerRemoving:Connect(function(player)
if script.Config.DeleteBuildsOnLeave.Value == true then
game.Workspace:FindFirstChild(player.Name):Destroy() -- Clean up the player's builds when they leave
end
end)
game.ReplicatedStorage.BreakBlock.OnServerEvent:Connect(function(player, part)
part:Destroy()
end)
game.ReplicatedStorage.PlaceBlock.OnServerEvent:Connect(function(player, name, size, color, position, Orientation, canCollide, anchored, parent)
local p = Instance.new("Part", parent)
p.Name = name
p.Size = size
p.Color = color
p.Position = position
p.Orientation = Orientation
p.CanCollide = canCollide
p.Anchored = anchored
p.TopSurface = Enum.SurfaceType.Smooth
p.BottomSurface = Enum.SurfaceType.Smooth
end)
Here’s my theory: The mouse.target is constantly switching between the platform and the baseplate causing it to keep jumping back and forth between the positions.
Any ideas as to why this might be happening?
(Oh and lastly I was wondering how you would make a snap to grid system for more than one stud. I managed to snap it to one stud with math.cecil but I was wondering how to do this with something like four studs.)
And yes I know my code is ugly don’t bully me