Roblox Studio错误:ServerScriptService.Script:15:尝试使用'CharacterAdded'索引nil

时间:2020-05-03 12:12:35

标签: lua roblox

我刚浏览一个脚本,却遇到一个错误,我真的不知道如何解决。如果有人可以帮忙,我将不胜感激! (第15行,尝试以'CharacterAdded'错误为索引nil。)

local TweenService = game:GetService("TweenService")
local blurDelay = 0 --seconds
local blurDuration = 6 --seconds
local blurEffect = Instance.new("BlurEffect")
blurEffect.Size = 0
blurEffect.Enabled = false
blurEffect.Parent = game.Workspace.CurrentCamera
local tween

local function createTween()
    local tweenInfo = TweenInfo.new(blurDuration, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, blurDelay)
    return TweenService:Create(blurEffect, tweenInfo, {Size = 56})
end

game.Players.LocalPlayer.CharacterAdded:Connect(function(character)
    local humanoid = character:WaitForChild("Humanoid")
    if tween and tween.PlaybackState == Enum.PlaybackState.Playing then
        tween:Cancel()
    end
    blurEffect.Size = 0
    blurEffect.Enabled = false
    humanoid.Died:Connect(function()
        blurEffect.Enabled = true
        tween = createTween()
        tween:Play()
    end)
end)

编辑:

  • 我试图将其切换为“本地脚本”,不仅仅是“ script”仍然无法正常工作。我试图将其从ServerScriptService切换到StarterGUI

2 个答案:

答案 0 :(得分:1)

您收到此错误,因为如果将此代码放入服务器上的常规Script中,则没有LocalPlayer。您需要在LocalScript中运行它,并将其放入StarterPlayerScripts。完全可以满足您的要求。

答案 1 :(得分:0)

不是game.Players.LocalPlayer.PlayerAdded,请改用game.Players.PlayerAdded