game.ReplicatedStorage.ClickEvent.OnServerEvent:Connect(function(Player,FromP,ToP) Player.PlayerGui.ScreenGui.ClickScript.Disabled = true local Cooldown = script.Parent.Cooldown.Value Cooldown = false local animation =Instance.new("Animation") animation.AnimationId = "http://www.roblox.com/Asset?ID="..3689511152 local loadedAnimation = script.Parent.Humanoid:LoadAnimation(animation) loadedAnimation:Play() wait(1.25) FromP = script.Parent.RightHand.Position ToP = ToP*-1 local RayCast = Ray.new(FromP, (ToP-FromP).unit*100) local Part, Position = game.Workspace:FindPartOnRay(RayCast, Player.Character, false, true) local beam = Instance.new("Part") beam.Name = "Beam" beam.Parent = game.Workspace beam.CanCollide = false beam.Anchored = true beam.CastShadow = false beam.BrickColor = BrickColor.new("Dark indigo") beam.Material = "Neon" local Dist = (ToP-FromP).magnitude beam.CFrame = CFrame.new(FromP, Position)*CFrame.new(0,0,Dist/2) beam.Size = Vector3.new(0.3,0.3,Dist) wait(0.05) beam.Size = Vector3.new(0.6,0.6,Dist) beam.Transparency = 0.8 wait(0.05) beam.Size = Vector3.new(0.9,0.9,Dist) beam.Transparency = 0.9 wait(0.05) beam.Size = Vector3.new(1.2,1.2,Dist) beam.Transparency = 0.99 wait(0.05) beam.Size = Vector3.new(1.6,1.6,Dist) beam.Transparency = 0.999 wait(0.05) beam.Size = Vector3.new(2.2,2.2,Dist) beam.Transparency = 0.9999 wait(0.1) beam.Size = Vector3.new(3,3,Dist) beam.Transparency = 0.99999 wait(0.1) beam.Size = Vector3.new(4.2,4.2,Dist) beam.Transparency = 0.999999 wait(0.1) beam.Size = Vector3.new(5.4,5.4,Dist) beam.Transparency = 0.9999999 wait(0.1) beam.Size = Vector3.new(6.6,6.6,Dist) wait(0.75) beam.Size = Vector3.new(7.8,7.8,Dist) beam.Transparency = 0.99999999 Cooldown = true wait(2) beam:remove() Player.PlayerGui.ScreenGui.ClickScript.Disabled = false end)
Hi there! I'm having a problem with this script (see above.) When I test it initially it works as expected until I start firing the laser I created multiple times and it starts to fire off in the same direction over and over again and will only move if I gradually move it to where I want it to be (which takes like 15 minutes.) I just dont understand how raycasting works, somebody please explain in detail.
{WHAT THE SCRIPT DOES}
Disables the client-side click script (see below) Tests if the player clicks from the client on another script and uses a remote event to get it to the server. Creates an animation and makes the player use it. Creates two vectors for the raycasting (and inverts the other so it doesnt fire backwards) Creates a ray. Makes the laser out of a part and sets some properties. Sets the part's position. Makes an animation by modifying the transparency and size of the laser over time. Waits a couple seconds and removes the beam. Enables the client-side click script (see below)
local UserInputService = game:GetService("UserInputService") local Mouse = game.Players.LocalPlayer:GetMouse() UserInputService.InputBegan:Connect(function(input) local inputType = input.UserInputType if inputType == Enum.UserInputType.MouseButton1 then game.ReplicatedStorage.ClickEvent:FireServer(game.ReplicatedStorage.ForBullets.CFrame.Position, Mouse.Hit.Position) end end)
Thanks for taking the time to get to this point in my question!