如何制作两次跳转脚本,使第二次跳转的力量成为玩家领导者统计值的值

时间:2019-06-13 17:16:54

标签: scripting roblox

我使这段代码想的是,它允许玩家跳两次,第二跳是其领导者统计的力量,但是它甚至不允许玩家第二次跳。

local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character
local humanoid

local canDoubleJump = false
local hasDoubleJumped = false
local oldPower
local time_delay = 0.2
local jump_multiplier = player.leaderstats.JumpBoost.Value

function onJumpRequest()
    if not character or not humanoid or not 
character:IsDescendantOf(workspace) or humanoid:GetState() == 
Enum.HumanoidStateType.Dead then
        return
    end

    if canDoubleJump and not hasDoubleJumped then
        hasDoubleJumped = true
        humanoid.JumpPower = oldPower * jump_multiplier
        humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
    end
end

local function characterAdded(new)
    character = new
    humanoid = new:WaitForChild("Humanoid")
    hasDoubleJumped = false
    canDoubleJump = false
    oldPower = humanoid.JumpPower

    humanoid.StateChanged:connect(function(old, new)
        if new == Enum.HumanoidStateType.Landed then
            canDoubleJump = false
            hasDoubleJumped = false
            humanoid.JumpPower = oldPower
        elseif new == Enum.HumanoidStateType.Freefall then
            wait(time_delay)
            canDoubleJump = true
        end
    end)
end

if player.Character then
    characterAdded(player.Character)    
end

player.CharacterAdded:connect(characterAdded)
UIS.JumpRequest:connect(onJumpRequest)

我希望玩家能够跳两次,而第二次跳具有领导者统计的能力(我只说了这一点,因为它说它想要更多细节)

1 个答案:

答案 0 :(得分:0)

LocalScript不在game.Workspace中执行:它们仅在客户端上运行,因此,术语“本地”;另一方面,Script是服务器端的。

您可以使用Script将双跳脚本(LocalScript)放入传入玩家的角色模型中。

-- Server-side Script
local players = game:GetService("Players")
local jumpScript = script:WaitForChild("JumpScript") -- Assume the double-jump script is a LocalScript called 'JumpScript'.

players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
        -- Check to make sure the character model does not already contain the double-jump script, just in case.
        local clone = char:FindFirstChild("JumpScript")
        if (not clone) then
            clone = jumpScript:Clone()
            clone.Parent = char
        end
    end)
end)

请注意,优良作法是将这样的服务器端脚本放入ServerScriptStorage而不是workspaceLighting中。 (您可以了解ServerScriptStorage here的安全性。)