I making a grid placement, please help!
Bug:
Module script in replicated storage:
local module = {}
local tween = game:GetService("TweenService")
local uis = game:GetService("UserInputService")
local runservice = game:GetService("RunService")
module.PlayingTweenNow = false
module.IsBuildingNow = false
module.FixedGrid = false
local function tweenModel(model, CF)
module.PlayingTweenNow = true
local CFrameValue = Instance.new("CFrameValue")
CFrameValue.Value = model.PrimaryPart.CFrame
CFrameValue:GetPropertyChangedSignal("Value"):Connect(function()
module.PlayingTweenNow = true
model:SetPrimaryPartCFrame(CFrameValue.Value)
end)
local tween = tween:Create(CFrameValue, TweenInfo.new(0.2, Enum.EasingStyle.Bounce), {Value = CF})
tween:Play()
tween.Completed:Connect(function()
CFrameValue:Destroy()
module.PlayingTweenNow = false
end)
end
function module.Rotate90(model)
model:SetPrimaryPartCFrame(model.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(90), 0))
end
local function antinongridblock(mouse)
if mouse.Target.Name == "GridBlock" or mouse.Target.Name == "RENAMEGRIDBLOCK!!!" then
module.FixedGrid = true
else
module.FixedGrid = false
end
end
function module.CreateGrid(AmoutOfBlocskRowHorizontallyAndVertically: number, Parent, DoTween: boolean,SizeOfBlock: number, DoMovetoCFame: boolean, MoveCFrame: CFrame, Anchor: boolean, CanCollide: boolean)
local modelgrid = Instance.new("Model")
modelgrid.Parent = Parent
modelgrid.Name = "Grid"
for x = SizeOfBlock, AmoutOfBlocskRowHorizontallyAndVertically * 10, SizeOfBlock do
for z = SizeOfBlock, AmoutOfBlocskRowHorizontallyAndVertically * 10, SizeOfBlock do
if DoTween == true then
task.wait(0.05)
end
-- to create a tween effect
local gridclone = Instance.new("Part") -- clones and keeps in workspace
local decal = Instance.new("Decal")
decal.Parent = gridclone
decal.Face = Enum.NormalId.Top
decal.Texture = "http://www.roblox.com/asset/?id=10640006995"
if DoMovetoCFame == true then
gridclone.Parent = modelgrid
else
gridclone.Parent = Parent
end
gridclone.Name = "GridBlock"
gridclone.Position = Vector3.new(x,0.5,z)
gridclone.Anchored = Anchor
gridclone.CanCollide = CanCollide
gridclone.Size = Vector3.new(SizeOfBlock,SizeOfBlock,SizeOfBlock)
end
end
task.wait(0.5)
if DoMovetoCFame == true then
modelgrid:PivotTo(MoveCFrame)
end
end
local function MoveModelPosition(model:Model, position: Vector3)
-- model:PivotTo(CFrame.new(position))
tweenModel(model,CFrame.new(position))
end
function module.snaptogrid(model, mouse)
-- for placing
antinongridblock(mouse)
if mouse.Target ~= nil then
if mouse.Target.Name == "GridBlock" then
if model.PrimaryPart then
module.FixedGrid = true
local target = mouse.Target
local TargetSurface = mouse.TargetSurface
if TargetSurface.Name == "Top" then
-- PlaceBlock.Position = target.Position + Vector3.new(0,3,0)--based on coordinates XYZ
MoveModelPosition(model, target.Position + Vector3.new(0,model.PrimaryPart.Size.X,0))
else
if TargetSurface.Name == "Bottom" then
-- PlaceBlock.Position = target.Position + Vector3.new(0,-3,0) -- just copy
MoveModelPosition(model, target.Position + Vector3.new(0,-model.PrimaryPart.Size.X,0))
else
if TargetSurface.Name == "Right" then
-- PlaceBlock.Position = target.Position + Vector3.new(3,0,0)
MoveModelPosition(model, target.Position + Vector3.new(model.PrimaryPart.Size.X,0,0))
else
if TargetSurface.Name == "Left" then
-- PlaceBlock.Position = target.Position + Vector3.new(-3,0,0)
MoveModelPosition(model, target.Position + Vector3.new(-model.PrimaryPart.Size.X,0,0))
else
if TargetSurface.Name == "Back" then
-- PlaceBlock.Position = target.Position + Vector3.new(0,0,3)
MoveModelPosition(model, target.Position + Vector3.new(0,0,model.PrimaryPart.Size.X))
else
if TargetSurface.Name == "Front" then
-- PlaceBlock.Position = target.Position + Vector3.new(0,0,-3)
MoveModelPosition(model, target.Position + Vector3.new(0,0,-model.PrimaryPart.Size.X))
end
end
end
end
end
end
end
end
end
end
function module.StartBuild(groupName,objectName, mouse: Mouse)
if module.IsBuildingNow == false then
module.IsBuildingNow = true
local group = game.ReplicatedStorage.Items:FindFirstChild(groupName)
local objecttomove = group:FindFirstChild(objectName):Clone()
objecttomove.Parent = workspace
objecttomove:FindFirstChild("GridBlock").Name = "RENAMEGRIDBLOCK!!!"
for i,p in pairs(objecttomove:GetDescendants()) do
if p:IsA("BasePart") or p:IsA("UnionOperation") or p:IsA("MeshPart") then
p.CanCollide = false
end
end
mouse.Move:Connect(function()
if module.IsBuildingNow ~= false then
module.snaptogrid(objecttomove, mouse)
end
end)
uis.InputBegan:Connect(function(key)
if key.KeyCode == Enum.KeyCode.R then
module.Rotate90(objecttomove)
end
end)
mouse.Button1Down:Connect(function()
if module.PlayingTweenNow == false then
if module.FixedGrid == true then
module.IsBuildingNow = false
if objecttomove.PrimaryPart then
game.ReplicatedStorage.Build:FireServer(objecttomove.Name, objecttomove.PrimaryPart.CFrame)
objecttomove:Remove()
end
end
end
end)
end
end
return module
Server script in serverscriptservice
function finditem(name)
for i,k in pairs(game.ReplicatedStorage.Items:GetDescendants()) do
if k:IsA("Model") then
if k.Name == name then
return k
end
end
end
end
game.ReplicatedStorage.Build.OnServerEvent:Connect(function(plr, objectname, cframe)
local func = finditem(objectname)
local object = func:Clone()
object.Parent = workspace
object:SetPrimaryPartCFrame(cframe)
end)
Local script in player gui:
local module = require(game.ReplicatedStorage.ModuleScript)
local mouse = game.Players.LocalPlayer:GetMouse()
local gui = script.Parent
function detectgroup()
for i,group in pairs(script.Parent.Groups:GetChildren()) do
if group:IsA("TextButton") then
group.MouseButton1Click:Connect(function()
for i,obj in pairs(script.Parent.Objects:GetChildren()) do
if obj:IsA("Frame") then
if obj:FindFirstChild("GroupName") then
if obj.GroupName.Value == group.Name then
obj.Visible = true
end
if obj.GroupName.Value ~= group.Name then
obj.Visible = false
end
end
end
end
end)
end
end
end
function detectclickobject()
for i,objbutton in pairs(script.Parent.Objects:GetChildren()) do
if objbutton:IsA("Frame") then
local findbutton = objbutton:FindFirstChildOfClass("TextButton")
findbutton.MouseButton1Click:Connect(function()
module.StartBuild(objbutton.GroupName.Value, objbutton.Name, mouse)
end)
end
end
end
for i,obj in pairs(game.ReplicatedStorage.Items:GetDescendants()) do
if obj:IsA("Model") then
local objview = obj:Clone()
local objbutton = script.OBJECTEXAMPLE:Clone()
objview.Parent = objbutton.view
objbutton.Parent = gui.Objects
objbutton.Name = obj.Name
objbutton.Cost.Text = obj.Cost.Value
objbutton.NameText.Text = obj.Name .. " ("..obj.Parent.Name..")"
objbutton.Visible = false
local str = Instance.new("StringValue")
str.Parent = objbutton
str.Name = "GroupName"
str.Value = obj.Parent.Name
if objview:IsA("Model") then
objview:PivotTo(objbutton.view.Pos.CFrame)
end
end
end
for i,g in pairs(game.ReplicatedStorage.Items:GetChildren()) do
local group = script.GroupButton:Clone()
group.Parent = script.Parent.Groups
group.Name = g.Name
group.Text = g.Name
group.Visible = true
end
task.wait(0.5)
detectgroup()
task.wait()
detectclickobject()
module.CreateGrid(20,workspace.Grid,false,10,false,CFrame.new(),true,false)