如何修复:尝试使用 'entered' 索引 nil
How to fix: attempt to index nil with 'entered'
所以基本上,脚本应该在介绍屏幕消失后加载文本,不确定它有什么问题,因为它在 2014 年就起作用了:/
代码:
if true then
script.Parent.slowsleigh:Play()
fadegui({intro.load},{0},{1},30)
wait(1)
local titles = intro.titles
local text1 = titles.title1.Text
local text2 = titles.title2.Text
local text3 = titles.title3.Text
local text4 = titles.title4.Text
if GameData.entered == 1 then
text1 = "And you came back."
text2 = "You can't change the past."
text3 = "Pathetic."
end
titles.title1.Text = ""
titles.title2.Text = ""
titles.title3.Text = ""
titles.title4.Text = ""
--[
titles.title1.Visible = true
for i = 1, #text1 do
titles.title1.Text = string.sub(text1,1,i)
wait(0.05 + math.random(-5,10)/200)
end
下面是 ServerScriptServices 中的脚本
local data = game:GetService("DataStoreService"):GetGlobalDataStore()
local dataVersion = 2
local func = Instance.new("RemoteFunction",workspace)
func.Name = "GetData"
function func.OnServerInvoke(player)
local store = data:GetAsync(tostring(player.UserId))
return store
end
local set = Instance.new("RemoteEvent",workspace)
set.Name = "SetData"
set.OnServerEvent:connect(function(player,newData)
local store = data:GetAsync(tostring(player.UserId))
for i,v in pairs(newData) do
store[i] = v
end
data:SetAsync(player.UserId, store)
end)
错误:
17:48:57.908 Players.Trl0_P90.PlayerGui.maingui.mainscript:758: attempt to index nil with 'entered' - Client - mainscript:758
错误告诉您您正在尝试访问 GameData.entered
,而 GameData
为 nil。
从您共享的代码来看,您的问题似乎是您正在访问数据存储中的数据,而没有考虑数据可能不存在的事实。查看 GlobalDataStore:GetAsync() 的文档:
This function returns the latest value of the provided key and a DataStoreKeyInfo instance. If the key does not exist or if the latest version has been marked as deleted, both return values will be nil.
这可能是新玩家加入导致的,没有默认数据。因此,您可以做的一件事是设置一些默认数据,以防出现新玩家:
local func = Instance.new("RemoteFunction",workspace)
func.Name = "GetData"
function func.OnServerInvoke(player)
local store = data:GetAsync(tostring(player.UserId))
-- if there is no data, it might be a new player!
-- provide some new player data
if not store then
store = {
entered = 1,
foo = true,
bar = "blah",
}
end
return store
end
这样,该函数将始终 return 形状正确的数据。
所以基本上,脚本应该在介绍屏幕消失后加载文本,不确定它有什么问题,因为它在 2014 年就起作用了:/
代码:
if true then
script.Parent.slowsleigh:Play()
fadegui({intro.load},{0},{1},30)
wait(1)
local titles = intro.titles
local text1 = titles.title1.Text
local text2 = titles.title2.Text
local text3 = titles.title3.Text
local text4 = titles.title4.Text
if GameData.entered == 1 then
text1 = "And you came back."
text2 = "You can't change the past."
text3 = "Pathetic."
end
titles.title1.Text = ""
titles.title2.Text = ""
titles.title3.Text = ""
titles.title4.Text = ""
--[
titles.title1.Visible = true
for i = 1, #text1 do
titles.title1.Text = string.sub(text1,1,i)
wait(0.05 + math.random(-5,10)/200)
end
下面是 ServerScriptServices 中的脚本
local data = game:GetService("DataStoreService"):GetGlobalDataStore()
local dataVersion = 2
local func = Instance.new("RemoteFunction",workspace)
func.Name = "GetData"
function func.OnServerInvoke(player)
local store = data:GetAsync(tostring(player.UserId))
return store
end
local set = Instance.new("RemoteEvent",workspace)
set.Name = "SetData"
set.OnServerEvent:connect(function(player,newData)
local store = data:GetAsync(tostring(player.UserId))
for i,v in pairs(newData) do
store[i] = v
end
data:SetAsync(player.UserId, store)
end)
错误:
17:48:57.908 Players.Trl0_P90.PlayerGui.maingui.mainscript:758: attempt to index nil with 'entered' - Client - mainscript:758
错误告诉您您正在尝试访问 GameData.entered
,而 GameData
为 nil。
从您共享的代码来看,您的问题似乎是您正在访问数据存储中的数据,而没有考虑数据可能不存在的事实。查看 GlobalDataStore:GetAsync() 的文档:
This function returns the latest value of the provided key and a DataStoreKeyInfo instance. If the key does not exist or if the latest version has been marked as deleted, both return values will be nil.
这可能是新玩家加入导致的,没有默认数据。因此,您可以做的一件事是设置一些默认数据,以防出现新玩家:
local func = Instance.new("RemoteFunction",workspace)
func.Name = "GetData"
function func.OnServerInvoke(player)
local store = data:GetAsync(tostring(player.UserId))
-- if there is no data, it might be a new player!
-- provide some new player data
if not store then
store = {
entered = 1,
foo = true,
bar = "blah",
}
end
return store
end
这样,该函数将始终 return 形状正确的数据。