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

Im making a gun, and now im adding a distance limit, something is wrong. Help?

Asked by
ZIRFAL3 17
3 years ago

As the title says, im making a gun, rn im trying to make a distance limit (how far can yo shoot).

Script:

script.Parent.Fire.OnServerEvent:Connect(function(player , mousePos , mouse)

    if script.Parent.equipped.Value == true then

        local Limit = 30
        local Limit2 = script.Parent.vector3Value.Value --This is the distance limit.
        local Pos = mousePos --Mouse Position

        print(Pos)

        local MouseHitPart = Instance.new("Part") --For the gun shooting im using a beam. 
        MouseHitPart.Parent = game.Workspace
        MouseHitPart.Anchored = true -- So the script creates a part in the mouse position.
        MouseHitPart.Transparency = 1
        MouseHitPart.CanCollide = false
        MouseHitPart.Size = Vector3.new(1,1,1)
        MouseHitPart.Position = mousePos

        if MouseHitPart.Position < Limit2  then -- Here's the problem, It says i cant compare 
                                                                              --this two things 
            local RayCastParams = RaycastParams.new()
        RayCastParams.FilterDescendantsInstances = {player.Character}
        RayCastParams.FilterType = Enum.RaycastFilterType.Blacklist

        local RayCastResult = workspace:Raycast(script.Parent.Handle.Position , (mousePos - script.Parent.Handle.Position)*300 , RayCastParams)

        local bullet = script.Parent.Beam1:Clone()
        bullet.Parent = script.Parent.Handle

        local bullet2 = script.Parent.Beam2:Clone()
        bullet2.Parent = script.Parent.Handle


        local Att1 = Instance.new("Attachment")
        Att1.Name = "Att1"
        Att1.Parent = script.Parent.Handle
        Att1.Position = Vector3.new(0,0,0)

        local Att2 = Instance.new("Attachment")
        Att2.Name = "Att2"
        Att2.Parent = MouseHitPart
        Att2.Position = Vector3.new(0,0,0)

        bullet.Attachment0 = Att1
        bullet.Attachment1 = Att2
        bullet2.Attachment0 = Att1
        bullet2.Attachment1 = Att2  

        if RayCastResult then
            local HitPart = RayCastResult.Instance
            local Model = HitPart:FindFirstAncestorOfClass("Model")

            if Model then
                if Model:FindFirstChildOfClass("Humanoid") then
                    Model.Humanoid.Health -= 30

                end
            end
        end

        wait(0.05)
        Att1:Destroy()
        Att2:Destroy()
        bullet:Destroy()
        bullet2:Destroy()
        MouseHitPart:Destroy()

        end

    --elseif 

    end




end)

So i made it so when you click, and you have clicked to the distance limit, it subtracts the number so it isn't past the limit. (Sorry if you cant understand, im bad with explanations).

0
Maybe you can name a variable and calculate the distance between the bullet and the start point? IDK. willywillycow 50 — 3y
0
thats what im trying to do, but the local Limit = 30 is not being considered as a Distance or Position. ZIRFAL3 17 — 3y

Answer this question