I created a simple building system, but the y axis is freaking out, and I get an error.
CODE:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Cube_1 = game.ReplicatedStorage.BuildingParts.Cube
local shadowCube = game.ReplicatedStorage.BuildingParts.Cube:Clone(); shadowCube.Transparency = 0.5; shadowCube.CanCollide = false; shadowCube.Parent = workspace
local runService = game:GetService("RunService")
local GRID_SIZE = 4
function snapToGrid(x)
return math.floor(x/GRID_SIZE + 0.5)*GRID_SIZE
end
function calculateY(toP, toS, oS)
return (toP + toS * 0.5) + oS * 0.5
end
runService.RenderStepped:Connect(function()
shadowCube.Position = Vector3.new(snapToGrid(mouse.Hit.Position.X), calculateY(mouse.Target.Position.Y, mouse.Target.Size.Y, Cube_1.Size.Y), snapToGrid(mouse.Hit.Position.Z))
end)
mouse.Button1Down:Connect(function()
local clone = Cube_1:Clone()
clone.Parent = workspace
clone.Position = shadowCube.Position
end)
ERROR:
VIDEO:
Scottifly
(Scottifly)
February 28, 2023, 8:39pm
2
The Part is being placed, the mouse is recognizing the Part you are placing and trying to place the Part, but when the new Part gets placed the old one gets removed.
Use Renderstepped but check to see if the grid Position changes, not the mouse Position.
What this should do is only place the Part when the grid position numbers change, not placing it every single frame .
Iām not really sure how to check for grid position, or at least implement it
Try using Mouse Target Filter .
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Cube_1 = game.ReplicatedStorage.BuildingParts.Cube
local shadowCube = game.ReplicatedStorage.BuildingParts.Cube:Clone(); shadowCube.Transparency = 0.5; shadowCube.CanCollide = false; shadowCube.Parent = workspace
local runService = game:GetService("RunService")
local GRID_SIZE = 4
mouse.TargetFilter = shadowCube -- tells the mouse to ignore shadowCube
function snapToGrid(x)
return math.floor(x/GRID_SIZE + 0.5)*GRID_SIZE
end
function calculateY(toP, toS, oS)
return (toP + toS * 0.5) + oS * 0.5
end
runService.RenderStepped:Connect(function()
shadowCube.Position = Vector3.new(snapToGrid(mouse.Hit.Position.X), calculateY(mouse.Target.Position.Y, mouse.Target.Size.Y, Cube_1.Size.Y), snapToGrid(mouse.Hit.Position.Z))
end)
mouse.Button1Down:Connect(function()
local clone = Cube_1:Clone()
clone.Parent = workspace
clone.Position = shadowCube.Position
end)
1 Like
system
(system)
Closed
March 14, 2023, 10:25pm
5
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.