This code is suppose to allow a player to create a rectangle by clicking, and then dragging to make the end of the rectangle follow their mouse and change size and angle to follow the mouse on the X, Z axis's. Then when they let go of their mouse, it shoots outwards. It works the first time, but if I try to do it again it can either not appear, create 2 of the object (or shake so much that it looks like 2, not sure), or not shoot out as far as it should or at the correct angle. Why is this happening?
local LocalPlayer = game.Players.LocalPlayer local mouse = LocalPlayer:GetMouse() local UserInputService = game:GetService("UserInputService") local clicking = false local boolean = false UserInputService.InputEnded:Connect(function(input) if input.UserInputType == Enum.UserInputType.MouseButton1 then clicking = false local beamLength = beamIndicator.Size.Z local direction = beamIndicator.CFrame.LookVector for i = 1, 100 do if i <= 30 then beamIndicator.Size = beamIndicator.Size - Vector3.new(-0.5, -0.5, 1 * beamLength / 10) end beamIndicator.CFrame = beamIndicator.CFrame - Vector3.new(direction.X * beamLength / 15, direction.Y * beamLength / 15, direction.Z * beamLength / 15) wait() end beamIndicator:Destroy() wait() boolean = false print("Loop Ended") end end) UserInputService.InputBegan:Connect(function(input) print("Input Started") if input.UserInputType == Enum.UserInputType.MouseButton1 then if boolean == true then return end boolean = true print("Clicked") clicking = true local StarterMousePosition = mouse.hit beamIndicator = Instance.new("Part", workspace.Projectiles) beamIndicator.Name = "BeamIndicator" beamIndicator.Anchored = true beamIndicator.Material = Enum.Material.Neon beamIndicator.CanCollide = false while clicking == true do wait() local CurrentMousePosition = mouse.hit local aimer = -(Vector3.new(StarterMousePosition.X, 3, StarterMousePosition.Z) - Vector3.new(CurrentMousePosition.X, 3, CurrentMousePosition.Z)) local distance = aimer.magnitude if distance >= 100 then distance = 100 end beamIndicator.Size = Vector3.new(1, 1, distance) beamIndicator.CFrame = CFrame.new(Vector3.new(StarterMousePosition.Position.X, 3, StarterMousePosition.Position.Z), Vector3.new(CurrentMousePosition.Position.X, 3, CurrentMousePosition.Position.Z)) * CFrame.new(0, 0, -distance / 2) print("Success") end end end)
Does this work for you?
local LocalPlayer = game.Players.LocalPlayer local mouse = LocalPlayer:GetMouse() local UserInputService = game:GetService("UserInputService") local clicking = false local boolean = false UserInputService.InputBegan:Connect(function(input) print("Input Started") if input.UserInputType == Enum.UserInputType.MouseButton1 then if boolean == true then return end boolean = true print("Clicked") clicking = true local StarterMousePosition = mouse.hit beamIndicator = Instance.new("Part", workspace.Projectiles) beamIndicator.Name = "BeamIndicator" beamIndicator.Anchored = true beamIndicator.Material = Enum.Material.Neon beamIndicator.CanCollide = false while clicking == true do wait() local CurrentMousePosition = mouse.hit local aimer = -(Vector3.new(StarterMousePosition.X, 3, StarterMousePosition.Z) - Vector3.new(CurrentMousePosition.X, 3, CurrentMousePosition.Z)) local distance = aimer.magnitude if distance >= 100 then distance = 100 end beamIndicator.Size = Vector3.new(1, 1, distance) beamIndicator.CFrame = CFrame.new(Vector3.new(StarterMousePosition.Position.X, 3, StarterMousePosition.Position.Z), Vector3.new(CurrentMousePosition.Position.X, 3, CurrentMousePosition.Position.Z)) * CFrame.new(0, 0, -distance / 2) print("Success") end end end) UserInputService.InputEnded:Connect(function(input) if input.UserInputType == Enum.UserInputType.MouseButton1 then clicking = false local beamLength = beamIndicator.Size.Z local direction = beamIndicator.CFrame.LookVector for i = 1, 100 do if i <= 30 then beamIndicator.Size = beamIndicator.Size - Vector3.new(-0.5, -0.5, 1 * beamLength / 10) end beamIndicator.CFrame = beamIndicator.CFrame - Vector3.new(direction.X * beamLength / 15, direction.Y * beamLength / 15, direction.Z * beamLength / 15) wait() end beamIndicator:Destroy() wait() boolean = false print("Loop Ended") end end)
I'm not the most elite, to be honest.