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

Velocity not working every time?(FIXED)

Asked by 4 years ago
Edited 4 years ago

Velocity not working every time?

I'm making a little game and it's based around throwing, the way I throw it is with velocity, it works in single player test/ local server test but it does not work on team test or in the actual Roblox game?

GIF

I really don't know why it does this!

I used THIS as a base

Local script: (fires remote)

local mouse = game.Players.LocalPlayer:GetMouse();
local hrp = game.Players.LocalPlayer.CharacterAdded:Wait():WaitForChild("HumanoidRootPart");
local bball = game.ReplicatedStorage.Dodgeball
local rs = game:GetService("RunService").RenderStepped;

local MaxVelocity = 65 -- max speed

local aval = 0.00001 -- arc size

local UIS = game:GetService("UserInputService")

local MB1Held = false
local MB1Helt = 0
local MaxHeldDown = 5

UIS.InputBegan:Connect(function(input, gameProcessed)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        MB1Held = true
        MB1Helt = tick()
        print("The left mouse button has been pressed!")
    end
end)

local attach0 = Instance.new("Attachment", game.Workspace.Terrain);
local attach1 = Instance.new("Attachment", game.Workspace.Terrain);

local beam = Instance.new("Beam", game.Workspace.Terrain);
beam.Attachment0 = attach0;
beam.Attachment1 = attach1;

local function beamProjectile(g, v0, x0, t1)
    -- calculate the bezier points
    local c = .5*.5*.5;
    local p3 = .5*g*t1*t1 + v0*t1 + x0;
    local p2 = p3 - (g*t1*t1 + v0*t1)/3;
    local p1 = (c*g*t1*t1 + 0.5*v0*t1 + x0 - c*(x0+p3))/(3*c) - p2;

    -- the curve sizes
    local curve0 = (p1 - x0).magnitude;
    local curve1 = (p2 - p3).magnitude;

    -- build the world CFrames for the attachments
    local b = (x0 - p3).unit;
    local r1 = (p1 - x0).unit;
    local u1 = r1:Cross(b).unit;
    local r2 = (p2 - p3).unit;
    local u2 = r2:Cross(b).unit;
    b = u1:Cross(r1).unit;

    local cf1 = CFrame.new(
        x0.x, x0.y, x0.z,
        r1.x, u1.x, b.x,
        r1.y, u1.y, b.y,
        r1.z, u1.z, b.z
    )

    local cf2 = CFrame.new(
        p3.x, p3.y, p3.z,
        r2.x, u2.x, b.x,
        r2.y, u2.y, b.y,
        r2.z, u2.z, b.z
    )

    return curve0, -curve1, cf1, cf2;
end

UIS.InputEnded:Connect(function(input, gameProcessed)

    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        MB1Held = false
        local t = 1
        local g = Vector3.new(0, -game.Workspace.Gravity, 0);
        local x0 = hrp.CFrame * Vector3.new(0, 2, -2)
        local Held = tick() - MB1Helt

        if Held >= 2.3 then
            Held = 2.3
        elseif Held < 1 then
            Held = 1
        end
        local v0 = (mouse.Hit.p - x0 - aval*g*t*t)/t;
        v0 = v0.unit * math.min(v0.magnitude, MaxVelocity)
        v0 = (v0 * Held)

        local curve0, curve1, cf1, cf2 = beamProjectile(g, v0, x0, t);

        game.ReplicatedStorage.Events.Game.ThrowDodgeball:FireServer(hrp, mouse.Hit.p, MB1Helt, Held, curve0, curve1, MB1Held) -- Humanoid root part, mouses hit pos, how long helt tick, calculated held, curve0 used for checking min size
    end
end)

game:GetService("RunService").RenderStepped:Connect(function(dt)
    if MB1Held == true then
        local t = 1

        local g = Vector3.new(0, -game.Workspace.Gravity, 0);
        local x0 = hrp.CFrame * Vector3.new(0, 2, -2)
        local Held = tick() - MB1Helt
        if Held >= 2.3 then
            Held = 2.3
        elseif Held < 1 then
            Held = 1
        end
        local v0 = (mouse.Hit.p - x0 - aval*g*t*t)/t;
        v0 = v0.unit * math.min(v0.magnitude, MaxVelocity)
        v0 = (v0 * Held)

        local curve0, curve1, cf1, cf2 = beamProjectile(g, v0, x0, t);
        beam.CurveSize0 = curve0;
        beam.CurveSize1 = curve1;
        if curve0  <= 12 then
            beam.Color = ColorSequence.new(Color3.new(1,0,0), Color3.new(1,0,0))
        else
            beam.Color = ColorSequence.new(Color3.new(0,1,0), Color3.new(0,1,0))
        end

        attach0.CFrame = attach0.Parent.CFrame:inverse() * cf1;
        attach1.CFrame = attach1.Parent.CFrame:inverse() * cf2;
    end
end)

Server Sided script:

local bball = game.ReplicatedStorage.Dodgeball
local rs = game:GetService("RunService").RenderStepped;
local MaxVelocity = 65 -- max arc speed before multiplaying with held time

local aval = 0.00001 -- arc size

game:GetService("ReplicatedStorage").Events.Game.ThrowDodgeball.OnServerEvent:Connect(function(plr, hrp, mousepos, MB1Helt, Held, curve0) -- player, Humanoid root part, mouses hit pos, how long helt tick, calculated held, curve0 used for checking min size
    local t = game:GetService("ServerStorage").DodgeBallValues.t.Value
    local g = Vector3.new(0, -game.Workspace.Gravity, 0);
    local x0 = hrp.CFrame * Vector3.new(0, 2, -2)
    local Held = tick() - MB1Helt
    if Held >= 2.3 then
        Held = 2.3
    elseif Held < 1 then
        Held = 1
    end
    local v0 = (mousepos - x0 - aval*g*t*t)/t; -- velocity
    v0 = v0.unit * math.min(v0.magnitude, MaxVelocity)
    v0 = (v0 * Held)

    if curve0  <= 12 then
        return false
    else
        if Held >= 2.3 then
            Held = 2.3
        elseif Held < 1 then
            Held = 1
        end
        local c = bball:Clone();
        c.DataVals:FindFirstChild("hmm_monkey").Value = tostring(plr.Name)
        c.ParticleEffect.Effect.Core.CanCollide = false
        c.ParticleEffect.Effect.Core.Attachment.Sparkle.Enabled = true
        c.ParticleEffect.Effect.Core.Attachment.Wave.Enabled = true
        c.ParticleEffect.Effect.Core.Attachment.Sparkle.Transparency = NumberSequence.new(1)
        c.ParticleEffect.Effect.Core.Attachment.Wave.Transparency = NumberSequence.new(1)
        print(Held)
        print("The left mouse button has been pressed!")
        -- have the ball travel that path
        c.Anchored = false
        c.Velocity = v0;
        c.CFrame = CFrame.new(x0);
        c.CanCollide = false;
        c.Parent = game.Workspace.ThrownDodgeBalls;
        wait(.1)
        c.CanCollide = true

        spawn(function()
            local runservivce = game:GetService("RunService")

            local function GetAllTouchingParts(part)
                local connection = part.Touched:Connect(function() end)
                local results = part:GetTouchingParts()
                connection:Disconnect()
                return results
            end



            -- Collision Detection
            local colas
            colas = runservivce.Heartbeat:Connect(function(step)
                local Touching = GetAllTouchingParts(c)
                if #Touching >= 1 then
                    c.ParticleEffect.Effect.Core.CFrame = c.CFrame
                    c.ParticleEffect.Effect.Core.Attachment.Sparkle.Transparency = NumberSequence.new(0)
                    c.ParticleEffect.Effect.Core.Attachment.Wave.Transparency = NumberSequence.new(0)
                    wait(.1)
                    print("cols kil")
                    c.ParticleEffect.Effect.Core.Attachment.Sparkle.Enabled = false
                    c.ParticleEffect.Effect.Core.Attachment.Wave.Enabled = false
                    wait(.1)
                    colas:Disconnect()
                end
            end)
        end)
    end
end)

Update:

it appears if you hold it down it acts as if you just tapped it? GIF I am going to see if any values are getting invalid on the server

Update 2:

I was testing and printing the values

    print(plr)
    print(hrp)
    print(mousepos)
    print(MB1Helt)
    print(tick() - MB1Helt)
    print(Held)
    print(curve0)

as I was doing this I saw online 18 I redeclared "Held" and from that test it would be approx .3 milliseconds off, commenting out line 18 fixed it!

Line 18:

    local Held = tick() - MB1Helt

Answer this question