英文:
Different Errors that when fixed just generate another Error- ROBLOX Studio
问题
以下是您要翻译的代码部分:
-- 下面的代码 --
local dead_bool = game.ServerScriptService.deadBool.Value
enemies_killed = game.StarterGui["Enemies Defeated"].Frame.enemydefeatednum.enemies_killed
print(dead_bool)
NPC_died = 0
while true do
wait(0.1)
if script.Parent.Humanoid ~= nil then
if script.Parent.Humanoid.Health <= 0 then
NPC_died += 1
dead_bool = true
enemies_killed = enemies_killed.Value + 1
print(enemies_killed)
wait(0.1)
end
end
end
请注意,这只是代码的翻译部分,不包括问题或其他内容。
英文:
So I am coding a game where on an NPC's death, The player's kills score goes up by 1 but there seems to be an error when I fix it all the time. It's either an 'Attempted to add Instance and Number' or 'Attempt to index number with 'Value'' Here is the code for you below (please note that the script is the NPC's Child.)
--code below --
enemies_killed = game.StarterGui["Enemies Defeated"].Frame.enemydefeatednum.enemies_killed
print(dead_bool)
NPC_died = 0
while true do
wait(0.1)
if script.Parent.Humanoid ~= nil then
if script.Parent.Humanoid.Health <= 0 then
NPC_died += 1
dead_bool = true
enemies_killed = enemies_killed.Value + 1
print(enemies_killed)
wait(0.1)
end
end
end
答案1
得分: 0
与设置所有这些不同,您可以使用事件来检测角色死亡并将1添加到玩家的击杀计数中。
基于您自己的脚本,这看起来可能是这样的:
script.Parent.Humanoid.Died:Once(function()
NPC_Died += 1
end)
然而,我个人会以不同的方式来处理:
而不是为每个NPC单独创建脚本,您可以创建一个使用事件来检测所有NPC的脚本,包括将来可能添加到文件夹中的任何NPC,并计算每个NPC死亡的次数,然后将其添加到击杀计数中。
local killCount = 0
local npcFolder = .../NPCFolder -- 无论在哪里
for i, npc in npcFolder:GetChildren() do
if npc and npc.Humanoid then
npc.Humanoid.Died:Once(function()
NPC_Died += 1
-- 在此处执行其他所需操作,例如使用碎片服务在一段时间后移除NPC
end
end
end
npcFolder.ChildAdded:Connect(function(child)
if child:FindFirstChildWhichIsA("Humanoid") then
-- 新的角色已添加到NPC文件夹
child.Humanoid.Died:Once(function()
NPC_Died += 1
end
end
end)
(很久没有使用RS,如果有错请见谅 😔)
英文:
Rather than setting this all up, you can use an event to detect when the humanoid dies and adding 1 to the player's killcount.
Based off your own script, that would look something like
script.Parent.Humanoid.Died:Once(function()
NPC_Died += 1
end)
However, I personally would approach this in a different way:
Instead of having a separate script for each NPC, you could have a script that will use events to detect for all NPCs, including any that may be added to the folder in the future, and count when each one dies and then adds it to the kill count.
local killCount = 0
local npcFolder = .../NPCFolder -- Where ever this is
for i, npc in npcFolder:GetChildren() do
if npc and npc.Humanoid then
npc.Humanoid.Died:Once(function()
NPC_Died += 1
-- do whatever else you want here, such as removing the NPC after a while using the debris service
end
end
end
npcFolder.ChildAdded:Connect(function(child)
if child:FindFirstChildWhichIsA("Humanoid") then
-- A new humanoid has been added to the NPC folder
child.Humanoid.Died:Once(function()
NPC_Died += 1
end
end
end)
(I haven't used RS in a long while sorry if i mistake 😔)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论