ROBLOX - 为什么这个飞脚本只能在Studio中运行?

时间:2015-08-06 16:18:01

标签: lua roblox

我真的需要帮助.. 我有这个代码用于飞行,作为背包项目:

Name = "Fly"
pi = 3.141592653589793238462643383279502884197163993751
a = 0
s = 0
ndist = 13
rs = 0.025
siz = Vector3.new(1, 1, 1)
form = 0
flow = {}
function CFC(P1,P2)
    local Place0 = CFrame.new(P1.CFrame.x,P1.CFrame.y,P1.CFrame.z) 
    local Place1 = P2.Position
    P1.Size = Vector3.new(P1.Size.x,P1.Size.y,(Place0.p - Place1).magnitude) 
    P1.CFrame = CFrame.new((Place0.p + Place1)/2,Place0.p)
end
function checktable(table, parentneeded)
    local i
    local t = {}
    for i = 1, #table do
        if table[i] ~= nil then
            if string.lower(type(table[i])) == "userdata" then
                if parentneeded == true then
                    if table[i].Parent ~= nil then
                        t[#t + 1] = table[i]
                    end
                else
                    t[#t + 1] = table[i]
                end
            end
        end
    end
    return t
end
if script.Parent.Name ~= Name then
    User = game:service("Players").Nineza
    HB = Instance.new("HopperBin")
    HB.Name = Name
    HB.Parent = User.StarterGear
    script.Parent = HB
    User.Character:BreakJoints()
end
speed = 50
script.Parent.Selected:connect(function(mar)
    s = 1
    torso = script.Parent.Parent.Parent.Character.Torso
    LeftShoulder = torso["Left Shoulder"]
    RightShoulder = torso["Right Shoulder"]
    LeftHip = torso["Left Hip"]
    RightHip = torso["Right Hip"]
    human = script.Parent.Parent.Parent.Character.Humanoid
    bv = Instance.new("BodyVelocity")
    bv.maxForce = Vector3.new(0,math.huge,0)
    bv.velocity = Vector3.new(0,0,0)
    bv.Parent = torso
    bg = Instance.new("BodyGyro")
    bg.maxTorque = Vector3.new(0,0,0)
    bg.Parent = torso 
    connection = mar.Button1Down:connect(function()
        a = 1
        bv.maxForce = Vector3.new(math.huge,math.huge,math.huge)
        bg.maxTorque = Vector3.new(900000,900000,900000)
        bg.cframe = CFrame.new(torso.Position,mar.hit.p) * CFrame.fromEulerAnglesXYZ(math.rad(-90),0,0)
        bv.velocity = CFrame.new(torso.Position,mar.hit.p).lookVector * speed
        moveconnect = mar.Move:connect(function()
            bg.maxTorque = Vector3.new(900000,900000,900000)
            bg.cframe = CFrame.new(torso.Position,mar.hit.p) * CFrame.fromEulerAnglesXYZ(math.rad(-90),0,0)
            bv.velocity = CFrame.new(torso.Position,mar.hit.p).lookVector * speed
        end)
        upconnect = mar.Button1Up:connect(function()
            a = 0
            moveconnect:disconnect()
            upconnect:disconnect()
            bv.velocity = Vector3.new(0,0,0)
            bv.maxForce = Vector3.new(0,math.huge,0)
            torso.Velocity = Vector3.new(0,0,0)
            bg.cframe = CFrame.new(torso.Position,torso.Position + Vector3.new(torso.CFrame.lookVector.x,0,torso.CFrame.lookVector.z))
            wait(1)
        end)
    end)
    while s == 1 do
        wait(0.02)
        flow = checktable(flow, true)
        local i
        for i = 1,#flow do
            flow[i].Transparency = flow[i].Transparency + rs
            if flow[i].Transparency >= 1 then flow[i]:remove() end
        end
        if a == 1 then
            flow[#flow + 1] = Instance.new("Part")
            local p = flow[#flow]
            p.formFactor = form
            p.Size = siz
            p.Anchored = true
            p.CanCollide = false
            p.TopSurface = 0
            p.BottomSurface = 0
            if #flow - 1 > 0 then
                local pr = flow[#flow - 1]
                p.Position = torso.Position - torso.Velocity/ndist
                CFC(p, pr)
            else
                p.CFrame = CFrame.new(torso.Position - torso.Velocity/ndist, torso.CFrame.lookVector)
            end
            p.BrickColor = BrickColor.new("Cyan")
            p.Transparency = 1
            p.Parent = torso
            local marm = Instance.new("BlockMesh")
            marm.Scale = Vector3.new(1.9, 0.9, 1.725)
            marm.Parent = p
            local amplitude
            local frequency
            amplitude = pi
            desiredAngle = amplitude
            RightShoulder.MaxVelocity = 0.4
            LeftShoulder.MaxVelocity = 0.4
            RightHip.MaxVelocity = pi/10
            LeftHip.MaxVelocity = pi/10
            RightShoulder.DesiredAngle = desiredAngle
            LeftShoulder.DesiredAngle = -desiredAngle
            RightHip.DesiredAngle = 0
            LeftHip.DesiredAngle = 0
        end
    end
end)
script.Parent.Deselected:connect(function()
    a = 0
    s = 0
    bv:remove()
    bg:remove()
    if connection ~= nil then
        connection:disconnect()
    end
    if moveconnect ~= nil then
        moveconnect:disconnect()
    end
    if upconnect ~= nil then
        upconnect:disconnect()
    end
    while s == 0 do
        wait()
        if #flow > 0 then
            flow = checktable(flow, true)
            local i
            for i = 1,#flow do
                flow[i].Transparency = flow[i].Transparency + rs
                if flow[i].Transparency >= 1 then flow[i]:remove() end
            end
        end
    end
end)
while true do
    wait()
    if s == 1 then
        return
    end
end
script:remove()

该脚本位于游戏的StarterPack文件夹中的HopperBin对象中。

现在,如果你自己尝试,你会发现它会起作用,但是如果你发布游戏,通过ROBLOX(不是工作室)播放并尝试使用该项目,你就不会飞。

任何想法为什么?

2 个答案:

答案 0 :(得分:3)

自从我没玩过ROBLOX已经很久了,所以我的回答可能不准确。

首先,您需要了解a Script and a Localscript之间的区别。简而言之,脚本运行服务器端,而Localscript运行客户端。您的飞行脚本应该在玩家的客户端上运行,因此您必须使用Localscript。

您的常规脚本在构建模式下工作,因为脚本在您的客户端上运行,因此被视为Localscripts。当您发布游戏并进行游戏时,您的计算机不再作为服务器工作,而是作为客户端。

除此之外,当您使用localscript时,您可能需要更改以下行:

-- gets the client (local player) on which the script is running
User = Game:GetService("Players").LocalPlayer

此外,请避免使用script.Parent.Parent.Parent....等相对路径。您想要获取客户端的躯干,因此只需按照User.Character.Torso使用您的用户变量即可。对角色的Humanoid对象执行相同的操作。

答案 1 :(得分:0)

我认为这是因为加载时间不存在,请尝试在第1行:

重复wait()直到game.Players.LocalPlayer.Character