Here is my code:
local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Char = Player.Character
local Humanoid = Char.Humanoid
local Animator = Humanoid:WaitForChild('Animator')
local PunchAnim = Instance.new('Animation')
PunchAnim.Name = 'PunchAnimation'
PunchAnim.Parent = Char
PunchAnim.AnimationId = 'rbxassetid://6741414062'
local ChopAnim = Instance.new('Animation')
ChopAnim.Name = 'ItemChop'
ChopAnim.Parent = Char
ChopAnim.AnimationId = 'rbxassetid://5686023191'
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local Mouse = Player:GetMouse()
local InstancesFilter = {}
local Camera = workspace.CurrentCamera
local Count = 0
local RunService = game:GetService('RunService')
local ServerStorage = game:GetService('ServerStorage')
local RunService = game:GetService('RunService')
local Placing = false
local Model
local Render
local Rotation = 0
local RotationIncrement = 30
local RangeHitBoxFound = false
for i,v in pairs(workspace:GetChildren()) do
local Found = v:FindFirstChild('Range')
local Found2 = v:FindFirstChild('RangeBox')
if Found then
table.insert(InstancesFilter,Found)
elseif Found2 then
table.insert(InstancesFilter,Found2)
end
Count += 1
if Count == 1000 then
RunService.RenderStepped:Wait()
Count = 0
end
end
workspace.ChildAdded:Connect(function(Child)
if Child:FindFirstChild('Range') then
table.insert(InstancesFilter,Child.Range)
elseif Child:FindFirstChild('RangeBox') then
table.insert(InstancesFilter,Child.RangeBox)
end
end)
local function MouseTarget()
local Parameters = RaycastParams.new()
Parameters.FilterDescendantsInstances = InstancesFilter
Parameters.FilterType = Enum.RaycastFilterType.Blacklist
local MouseVector2Pos = UIS:GetMouseLocation()
local UnitRay = Camera:ScreenPointToRay(MouseVector2Pos.X,MouseVector2Pos.Y)
local RaycastResult = workspace:Raycast(UnitRay.Origin,UnitRay.Direction * 500,Parameters)
if not RaycastResult then return nil end
return RaycastResult.Instance
end
local function MouseHit(Structure)
if Structure then
local Parameters = RaycastParams.new()
Parameters.FilterDescendantsInstances = {Structure}
Parameters.FilterType = Enum.RaycastFilterType.Blacklist
local MouseVector2Pos = UIS:GetMouseLocation()
local UnitRay = Camera:ScreenPointToRay(MouseVector2Pos.X,MouseVector2Pos.Y)
local RaycastResult = workspace:Raycast(UnitRay.Origin,UnitRay.Direction * 500,Parameters)
if not RaycastResult then return nil end
return RaycastResult.Position
else
local Parameters = RaycastParams.new()
Parameters.FilterDescendantsInstances = InstancesFilter
Parameters.FilterType = Enum.RaycastFilterType.Blacklist
local MouseVector2Pos = UIS:GetMouseLocation()
local UnitRay = Camera:ScreenPointToRay(MouseVector2Pos.X,MouseVector2Pos.Y)
local RaycastResult = workspace:Raycast(UnitRay.Origin,UnitRay.Direction * 500,Parameters)
if not RaycastResult then return nil end
return RaycastResult.Position
end
end
UIS.InputBegan:Connect(function(Input,GameProcessed)
if not GameProcessed and Input.UserInputType == Enum.UserInputType.MouseButton1 and ReplicatedStorage.Completed.Value then
if Placing then
Placing = false
Render:Disconnect()
ReplicatedStorage.PlaceStructure:FireServer(Model.Name,Model.PrimaryPart.CFrame)
Model:Destroy()
Model = nil
else
local Target = MouseTarget()
if Target == nil then return end
local MousePos = MouseHit()
if MousePos == nil then
MousePos = Mouse.Hit.p
end
ReplicatedStorage.ChopSlashPunch:FireServer(MouseHit(),Target)
if Target.Parent.Name == 'Tree' then
if not Player:GetAttribute('Cooldown') then
local AnimTrack
if Char:FindFirstChildWhichIsA('Tool') then
AnimTrack = Animator:LoadAnimation(ChopAnim)
else
AnimTrack = Animator:LoadAnimation(PunchAnim)
end
AnimTrack:Play()
end
elseif Target.Parent.Name == 'Zombie' then
if not Player:GetAttribute('AttackCooldown') then
local AnimTrack
if Char:FindFirstChildWhichIsA('Tool') then
AnimTrack = Animator:LoadAnimation(ChopAnim)
else
AnimTrack = Animator:LoadAnimation(PunchAnim)
end
AnimTrack:Play()
end
end
end
end
end)
ReplicatedStorage.BlueprintStructure.OnClientEvent:Connect(function(Structure)
local Found = ReplicatedStorage.Structures:FindFirstChild(Structure)
Model = Found:Clone()
Model.Parent = workspace
Placing = true
for i,v in pairs(Model:GetDescendants()) do
if v:IsA('BasePart') and v.Name ~= 'Center' then
v.Transparency = 0.5
v.BrickColor = BrickColor.new('Forest green')
v.CanCollide = false
end
end
Render = RunService.RenderStepped:Connect(function(Step)
if UIS:IsKeyDown(Enum.KeyCode.R) then
Rotation = Rotation + (Step * RotationIncrement)
end
local Pos = MouseHit(Model)
if Pos == nil then
Pos = Mouse.Hit.p
end
Model:SetPrimaryPartCFrame(CFrame.new(Pos) * CFrame.Angles(0,math.rad(Rotation),0))
local TouchedConnections = {}
local TouchingParts = {}
for i,v in pairs(Model:GetDescendants()) do
if v:IsA('BasePart') then
table.insert(TouchedConnections,v.Touched)
for i2,v2 in pairs(v:GetTouchingParts()) do
table.insert(TouchingParts,v2)
end
end
end
for i,v in pairs(TouchingParts) do
if v.Name == 'Range' then
RangeHitBoxFound = true
break
end
end
for i,v in pairs(Model:GetDescendants()) do
if v:IsA('BasePart') then
v.BrickColor = RangeHitBoxFound and BrickColor.new('Really red') or BrickColor.new('Forest green')
end
end
end)
end)
When placing is true and UIS detects a mouse click, it has a line for Render:Disconnect() but when I tested it myself the structure kept setting its position to my mouse position even after placing was set to false and Render:Disconnect() was called.