设置人形角色行走速度

我在类似的问题中找到了此脚本,但是我遇到了错误 16:11:18.560 - Workspace.Script:2: attempt to index nil with 'Character'

local Player = game:GetService("Players").LocalPlayer
local character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
-- 设置速度
local Humanoid =  character:WaitForChild("Humanoid")
if Humanoid then
Humanoid.WalkSpeed = 25
end

有人可以帮帮我吗?

点赞
用户2860267
用户2860267

LocalPlayer对象只存在于LocalScripts中。这就是为什么你的变量Player为空的原因。

有两种方法可以解决这个问题:

1)将这段代码移入LocalScript中,或者

2)将这段代码添加到玩家加入游戏时执行的回调函数中。代码如下:

local PlayerService = game:GetService("Players")

-- 等待玩家加入游戏
PlayerService.PlayerAdded:Connect(function(Player)
    -- 等待玩家角色加载
    Player.CharacterAdded:Connect(function(Character)
        -- 设置移动速度
        local Humanoid = Character:WaitForChild("Humanoid")
        if Humanoid then
            Humanoid.WalkSpeed = 25
        end
    end)
end)
2020-04-18 16:09:47
用户20366950
用户20366950

或者制作一个能让你速度增加的砖:

local KillBrick = script.Parent()

KillBrick.Touched(function(kill)
 local humanoid = kill.Parent:FindFirstChild("humanoid")
 if humanoid then
  humanoid.WalkSpeed = 25
end

end)
2022-11-21 21:40:43