Hey, ya! I was kind of using a placement tutorial thing, but I changed some of it for my usage.
-
What do you want to achieve? I was making a placement system for fun.
-
What is the issue? In the tutorial it was placing towers (its a human model) however when I change the human model to a custom model (What I actually want to place) it breaks. The object won’t place in the correct position at all.
Placing the character Model Placing Character Model - YouTube
Placing custom Model Placing Custom Model - YouTube
- What solutions have you tried so far? I’ve used print statements throughout the entire thing to try and figure what the problem is, but nothing works.
*Below, in the local script there is ONE line that I HAVE to change for it to not error at me:
For placing a player character
local y = result.Position.Y + objectToSpawn.Humanoid.HipHeight + (objectToSpawn.PrimaryPart.Size.Y / 2)
For the custom model
local y = result.Position.Y
Local Script (Inside a GUI element)
local replicatedStorage = game:GetService("ReplicatedStorage")
local runService = game:GetService("RunService")
local UIS = game:GetService("UserInputService")
local physicsService = game:GetService("PhysicsService")
local events = replicatedStorage:WaitForChild("Events")
local remoteEvent = events:WaitForChild("RemoteEvent")
local spawnObject_Event = remoteEvent:WaitForChild("SpawnObject")
local camera = workspace.CurrentCamera
local gui = script.Parent
local objectToSpawn = nil
local canPlace = false
local rotation = 0
local objs = replicatedStorage:WaitForChild("Objects")
local function MouseRaycast(blacklist)
local mousePosition = UIS:GetMouseLocation()
local mouseRay = camera:ViewportPointToRay(mousePosition.X, mousePosition.Y)
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = blacklist
local raycastResult = workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 1000, raycastParams)
return raycastResult
end
local function removePlaceholderObj()
if objectToSpawn then
objectToSpawn:Destroy()
objectToSpawn = nil
rotation = 0
end
end
local function AddPlaceholderObj(name)
local objExists = objs:FindFirstChild(name)
if objExists then
removePlaceholderObj()
objectToSpawn = objExists:Clone()
objectToSpawn.Parent = workspace.PlacedObjects
for i, object in ipairs(objectToSpawn:GetDescendants()) do
if object:IsA("BasePart") then
physicsService:SetPartCollisionGroup(object, "Object")
object.Material = Enum.Material.ForceField
end
end
end
end
local function ColourPlaceholderObj(colour)
for i, object in ipairs(objectToSpawn:GetDescendants()) do
if object:IsA("BasePart") then
object.Color = colour
end
end
end
gui.Activated:Connect(function()
AddPlaceholderObj("Cone")
end)
UIS.InputBegan:Connect(function(input, processed)
if processed then
return
end
if objectToSpawn then
if input.UserInputType == Enum.UserInputType.MouseButton1 then
if canPlace then
spawnObject_Event:FireServer(objectToSpawn.Name, objectToSpawn.PrimaryPart.CFrame)
print("IMPORTANT")
print(objectToSpawn.PrimaryPart.CFrame)
removePlaceholderObj()
end
elseif input.KeyCode == Enum.KeyCode.R then
rotation += 90
end
end
end)
runService.RenderStepped:Connect(function()
if objectToSpawn then
local result = MouseRaycast({objectToSpawn})
if result and result.Instance then
if result.Instance.Parent.Name == "ObjectArea" then
canPlace = true
ColourPlaceholderObj(Color3.new(0,1,0))
else
canPlace = false
ColourPlaceholderObj(Color3.new(1,0,0))
end
local x = result.Position.X
local y = result.Position.Y -- + objectToSpawn.Humanoid.HipHeight + (objectToSpawn.PrimaryPart.Size.Y / 2)
local z = result.Position.Z
local cframe = CFrame.new(x,y,z) * CFrame.Angles(0, math.rad(rotation), 0)
objectToSpawn:SetPrimaryPartCFrame(cframe)
end
end
end)
Model/Server Script
local physicsService = game:GetService("PhysicsService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local events = replicatedStorage:WaitForChild("Events")
local remoteEvent = events:WaitForChild("RemoteEvent")
local spawnObject_Event = remoteEvent:WaitForChild("SpawnObject")
local object = {}
function object.Spawn(player, name, cframe)
local objectExists = replicatedStorage.Objects:FindFirstChild(name)
if objectExists then
print("IMPORTANT-2")
print(cframe)
local newObject = objectExists:Clone()
newObject.Root.CFrame = cframe -- newObject.Root.CFrame = cframe
newObject.Parent = workspace.PlacedObjects
newObject.PrimaryPart:SetNetworkOwner(nil)
for i, object in ipairs(newObject:GetDescendants()) do
if object:IsA("BasePart") then
physicsService:SetPartCollisionGroup(object, "Object")
end
end
else
warn("Requested Object does not exist: ", name)
end
end
spawnObject_Event.OnServerEvent:Connect(object.Spawn)
return object