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

Not even sure what's happening, very confusing?

Asked by 4 years ago

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)

0
btw, that's cool LittleGabieleRob 5 — 4y
0
Don't set the parent of the object in the Instance.new(), it has been deprecated for many years and is a bad practice unless you aren't planning on setting any of the object's properties. SteamG00B 1633 — 4y
0
Also, in InputEnded, what is beamIndicator? I don't see you set that variable anywhere before you use it or outside of the function. SteamG00B 1633 — 4y
0
beamIndicator is the object being created in the function that runs first. I figured that by the time the InputEnded function had run, the variable would already be set because the first function had run, but maybe this isn't the case? Not sure. Also, wow I had no idea that assigning the parent like that was deprecated! eyeball001 7 — 4y
View all comments (2 more)
0
The whole thing works correctly the first time I assumed that wasn't the problem.. eyeball001 7 — 4y
0
Never mind, It has been fixed. eyeball001 7 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

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.

0
Same results. eyeball001 7 — 4y
Ad

Answer this question