#Placement System problem
1 messages · Page 1 of 1 (latest)
this the script
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local Runservice = game:GetService("RunService")
local player = game:GetService("Players")
local localPlayer = player.LocalPlayer
local Mouse = localPlayer:GetMouse()
local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")
local camera = game.Workspace.CurrentCamera
local IsPlacing = false
local Template = nil
local Character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
local Grid_Size = 4
local function GridSize(position)
return Vector3.new(
math.floor(position.X / Grid_Size + 0.5) * Grid_Size,
math.floor(position.Y / Grid_Size + 0.5) * Grid_Size,
math.floor(position.Z / Grid_Size + 0.5) * Grid_Size
)
end
UserInputService.InputBegan:Connect(function(input,gpe)
if input.KeyCode == Enum.KeyCode.E then
if IsPlacing == false then
IsPlacing = true
Template = ReplicatedStorage:WaitForChild("GreenTemplate"):Clone()
for _,v in pairs(Template:GetChildren()) do
v.CanCollide = false
v.Anchored = true
v.Transparency = 0.5
end
Template.Parent = workspace
else
IsPlacing = false
if Template then
Template:Destroy()
Template = nil
end
end
end
end)
Runservice.Heartbeat:Connect(function()
if not IsPlacing or not Template then return end
local unitRay = camera:ScreenPointToRay(Mouse.X, Mouse.Y)
local Origin = unitRay.Origin
local Direction = unitRay.Direction * 50
local Params = RaycastParams.new()
Params.FilterDescendantsInstances = {Character}
Params.FilterType = Enum.RaycastFilterType.Exclude
Params.IgnoreWater = true
local result = workspace:Raycast(Origin,Direction,Params)
if result then
local normal = result.Normal
if result.Normal:Dot(Vector3.new(0, 1, 0)) > 0.95 then
local SnapPos = GridSize(result.Position)
Template:SetPrimaryPartCFrame(CFrame.new(SnapPos + Vector3.new(0, Template.PrimaryPart.Size.Y / 2, 0)))
end
end
end)
btw i cut it cuz im not able to send full one
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local Runservice = game:GetService("RunService")
local player = game:GetService("Players")
local localPlayer = player.LocalPlayer
local Mouse = localPlayer:GetMouse()
local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")
local camera = game.Workspace.CurrentCamera
local IsPlacing = false
local Template = nil
local Character = localPlayer.Character or localPlayer.CharacterAdded:Wait()
local Grid_Size = 4
local function GridSize(position)
return Vector3.new(
math.floor(position.X / Grid_Size + 0.5) * Grid_Size,
math.floor(position.Y / Grid_Size + 0.5) * Grid_Size,
math.floor(position.Z / Grid_Size + 0.5) * Grid_Size
)
end
UserInputService.InputBegan:Connect(function(input,gpe)
if input.KeyCode == Enum.KeyCode.E then
if IsPlacing == false then
IsPlacing = true
Template = ReplicatedStorage:WaitForChild("GreenTemplate"):Clone()
for _,v in pairs(Template:GetChildren()) do
v.CanCollide = false
v.Anchored = true
v.Transparency = 0.5
end
Template.Parent = workspace
else
IsPlacing = false
if Template then
Template:Destroy()
Template = nil
end
end
end
end) ```
Runservice.Heartbeat:Connect(function()
if not IsPlacing or not Template then return end
local unitRay = camera:ScreenPointToRay(Mouse.X, Mouse.Y)
local Origin = unitRay.Origin
local Direction = unitRay.Direction * 50
local Params = RaycastParams.new()
Params.FilterDescendantsInstances = {Character}
Params.FilterType = Enum.RaycastFilterType.Exclude
Params.IgnoreWater = true
local result = workspace:Raycast(Origin,Direction,Params)
if result then
local normal = result.Normal
if result.Normal:Dot(Vector3.new(0, 1, 0)) > 0.95 then
local SnapPos = GridSize(result.Position)
local modelSize = Template:GetExtentsSize()
local modelBottom = Template:GetBoundingBox().Position.Y - modelSize.Y / 2
local primaryBottom = Template.PrimaryPart.Position.Y - Template.PrimaryPart.Size.Y / 2
local offset = primaryBottom - modelBottom
Template:SetPrimaryPartCFrame(CFrame.new(SnapPos + Vector3.new(0, modelSize.Y / 2 + offset, 0)))
end
end
end)
@spare oar try this