Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Raycast firing in random directions over time. How to fix?

Asked by 5 years ago

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!

0
Could you please be a bit more specific on what you're trying to do? You want it to be a first person shooter or the bullet go according to the mouse? User#27525 1 — 5y
0
to go according to the mouse tdljayden 1 — 5y
0
it plays a little animation and then the beam shoots from the "RightHand" to the mouse.hit.position tdljayden 1 — 5y
0
ive done some research and ive seen that mouse.hit.position is a legacy api, and they said it could be deprecated. so im changing to userinputservice tdljayden 1 — 5y

Answer this question