我在一个类似的问题中找到了这个脚本,但是,我得到了错误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")
-- setting speed
local Humanoid = character:WaitForChild("Humanoid")
if Humanoid then
Humanoid.WalkSpeed = 25
end有谁可以帮我?
发布于 2020-04-19 00:09:47
LocalPlayer对象仅存在于LocalScripts中。这就是您的变量Player为空的原因。
有两种方法可以解决这个问题:
1)将此代码移动到LocalScript中,或者
2)将此代码添加到玩家加入游戏时执行的回调中。这就是它看起来的样子。
local PlayerService = game:GetService("Players")
-- wait for a player to join the game
PlayerService.PlayerAdded:Connect( function(Player)
-- wait for the player's character to load
Player.CharacterAdded:Connect( function(Character)
-- set the speed
local Humanoid = Character:WaitForChild("Humanoid")
if Humanoid then
Humanoid.WalkSpeed = 25
end
end)
end)https://stackoverflow.com/questions/61290720
复制相似问题