不同的错误,在修复时只会生成另一个错误 – ROBLOX Studio

huangapple go评论68阅读模式
英文:

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[&quot;Enemies Defeated&quot;].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 &lt;= 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(&quot;Humanoid&quot;) 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 😔)

huangapple
  • 本文由 发表于 2023年7月20日 09:53:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定