I’m making a TDS styled game, and I just finished the placement system. I built mine based on @starmaq’s system from his tutorial, but for some reason my towers keep glitching & spasming around when they are going to be placed near the camera. How do I prevent this from happening?
Client-side code:
local repStorage = game:GetService("ReplicatedStorage")
local runService = game:GetService("RunService")
local isPlacing = false
local place = repStorage:WaitForChild("PlaceTower")
local startPlacing = repStorage:WaitForChild("StartPlace")
local isPlacingEvent = repStorage:WaitForChild("IsPlacing")
local rotateEvent = repStorage:WaitForChild("Rotate")
local UserInputService = game:GetService("UserInputService")
local plrService = game:GetService("Players")
local PlaceConnection
local RotateConnection
local plr = plrService.LocalPlayer
script.Parent.MouseButton1Up:Connect(function()
print("Clicked!")
print(isPlacing)
if isPlacing == false then
isPlacing = true
local mouse = plr:GetMouse()
local turret = repStorage:WaitForChild("Turret")
startPlacing:FireServer(turret)
print("Fired from client!")
PlaceConnection = runService.RenderStepped:Connect(function()
isPlacingEvent:FireServer(mouse.Hit.Position.X, mouse.Hit.Position.Z)
end)
RotateConnection = UserInputService.InputBegan:Connect(function(input, gpu)
if gpu then return end
if input.KeyCode == Enum.KeyCode.R then
rotateEvent:FireServer()
end
end)
local downConnection = mouse.Button1Down:Connect(function()
place:FireServer()
RotateConnection:Disconnect()
PlaceConnection:Disconnect()
isPlacing = false
end)
end
end)
Server-side code:
local repStorage = game:GetService("ReplicatedStorage")
local tweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Quint)
local startPlacing = repStorage:WaitForChild("StartPlace")
local place = repStorage:WaitForChild("PlaceTower")
local isPlacing = repStorage:WaitForChild("IsPlacing")
local runService = game:GetService("RunService")
local rotateEvent = repStorage:WaitForChild("Rotate")
local RotateConnection
local finishedTween = false
local toPlace
local placeFunction = function(tower, x, z, below) --The place function
local firstPos = (((Vector3.new(0, below.Position.Y+(below.Size.Y/2), 0))+(Vector3.new(x, 0, z)*1.1)+Vector3.new(0, tower.Size.Y/2, 0)))*1.1
tower.Position = (firstPos - Vector3.new(0, firstPos.Y, 0))+(Vector3.new(0, tower.Size.Y/2, 0))
end
startPlacing.OnServerEvent:Connect(function(plr, tower)
print("Received from server!")
toPlace = tower:Clone()
toPlace.CanCollide = false
toPlace.Transparency = 0.5
toPlace.Parent = game.Workspace
toPlace.Name = "Turret"
isPlacing.OnServerEvent:Connect(function(plr, x, z)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {toPlace}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.IgnoreWater = true
local rayCast = game.Workspace:Raycast((Vector3.new(x, 0, z)*1.25)+Vector3.new(0, tower.Size.Y/2, 0), Vector3.new(0, -100, 0), raycastParams)
local instance = rayCast.Instance or game.Workspace.Baseplate
placeFunction(toPlace, x, z, rayCast.Instance)
end)
end)
rotateEvent.OnServerEvent:Connect(function(plr)
if not finishedTween then
finishedTween = true
local tween = tweenService:Create(toPlace, tweenInfo, {Orientation = toPlace.Orientation + Vector3.new(0, 90, 0)})
tween:Play()
tween.Completed:Wait()
finishedTween = false
end
end)
place.OnServerEvent:Connect(function(plr)
toPlace.CanCollide = true
toPlace.Transparency = 0
end)
Note that everything works, I just want to stop my towers from spasming when I’m placing them near the camera