Essentially what I am trying to accomplish is a block placement system, in which you can place 4x4x4 blocks by clicking. However, it occasionally just stops working. However, even then, it still prints “Test” even while doing it, meaning that it should be placing blocks.
The video that displays this can be seen here. (the file size is too large to fit in here)
If you did watch the video, you could see that it would work however sometimes it would also just refuse to place any blocks whatsoever, regardless of if its in range.
(Keep in mind that the code is client-sided for now, once I figure out how to properly place blocks I will make it Filter-Enabled compatible.)
local Player = game:GetService("Players").LocalPlayer
local Mouse = Player:GetMouse()
local Character = Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Humanoid = Character:WaitForChild("Humanoid")
local Tool = script.Parent
function roundBlockPlacement(Position, Object)
local X,Y,Z
X = math.floor(Position.X / Object.Size.X + .5) * Object.Size.X
Y = math.floor(Position.Y / Object.Size.Y + .5) * Object.Size.Y
Z = math.floor(Position.Z / Object.Size.Z + .5) * Object.Size.Z
return Vector3.new(X,Y,Z)
end
function toolActivated()
if (Mouse.Hit.Position - HumanoidRootPart.Position).Magnitude <= 40 then
print("Test")
local mouseRay = Mouse.UnitRay
local newRay = Ray.new(mouseRay.Origin, mouseRay.Direction.Unit * 40)
local touchedPart, mousePosition, partLookVector = workspace:FindPartOnRay(newRay)
local Part = Instance.new("Part")
Part.Size = Vector3.new(4,4,4)
Part.Anchored = true
if touchedPart and mousePosition and partLookVector then
local partPosition = roundBlockPlacement(mousePosition + (partLookVector * (Part.Size *.5)), Part)
Part.CFrame = CFrame.new(partPosition)
Part.Parent = workspace
end
end
end
Tool.Activated:Connect(toolActivated)