Lua Roblox如何修复If语句?
我目前正在为我的游戏设计一种武器,但它有一些问题。武器不是工具,而是连接到角色。我制作了以下脚本来检查武器是否装备,然后允许玩家攻击Lua Roblox如何修复If语句?,lua,roblox,Lua,Roblox,我目前正在为我的游戏设计一种武器,但它有一些问题。武器不是工具,而是连接到角色。我制作了以下脚本来检查武器是否装备,然后允许玩家攻击 Mouse.Button1Down:connect(function() if isEquipped == true then if not pause then pause = true anim1:Play() wait(0.1) trail
Mouse.Button1Down:connect(function()
if isEquipped == true then
if not pause then
pause = true
anim1:Play()
wait(0.1)
trail.Enabled = true
wait(0.6)
trail.Enabled = false
pause = false
end
end
end)
handle.Touched:connect(function(hit)
if isEquipped == true then
if not pause2 then
pause2 = true
if Mouse.Button1Down then
if humanoid and humanoid.Health > 0 and hit and not hit:isDescendantOf(person) then
local target = hit.Parent:FindFirstChild("Humanoid")
if target and target.Health > 0 then
target:TakeDamage(damage)
wait(0.7)
pause2 = false
end
end
end
end
end
end)
game.Players.LocalPlayer:GetMouse().KeyDown:Connect(function(KeyPressed)
if KeyPressed == "e" then
if not pause3 then
pause3 = true
if toggle == false then
toggle = true
isEquipped.Value = true
fake1.Transparency = 1
fake2.Transparency = 1
disc1.Transparency = 0
disc2.Transparency = 0
wait(0.7)
pause3 = false
else
pause3 = true
toggle = false
isEquipped.Value = false
fake1.Transparency = 0
fake2.Transparency = 0
disc1.Transparency = 1
disc2.Transparency = 1
wait(0.7)
pause3 = false
end
end
end
end)
问题是我可以装备武器,但装备后,我无法用它攻击。
如果你能帮忙,我将不胜感激。提前谢谢 在您显示的代码中,没有一段或部分代码实际将isEquipped设置为true。您应该将此值绑定到
tool.confided
事件
如果您想了解更多信息,我建议您访问Roblox Wiki
另外,您使用的是本地脚本还是脚本?如果您使用的是LocalScript,则应使用RemoteEvents处理影响其他客户端的事情,例如声音、设置部件属性、创建部件等。您可以在Roblox Wiki中找到有关此客户端-服务器通信模型的更详细信息
另外,当程序启动时,
pause3
的值是多少?这个变量的赋值是多少?程序启动后,应该设置为false,否则将无法攻击。鼠标。KEYDOWN被弃用,考虑使用不同的获取输入的方法:这也是您的脚本吗?或者它是一个freemodel脚本?我猜响应不会很快出现在这里,因此,出于同样的原因,您会在自己的代码中发现逻辑错误:深度嵌套的if
语句。这并不是你问题的答案,但如果你减少了筑巢,你可能会得到答案。至少,使用一些其他的else
语句来捕捉意外行为。@coordinarenewton这是我的脚本,但我使用了Roblox战斗剑的伤害方法。仍然需要一个答案。要调试这个程序,我建议在每个if语句之后放置一个print语句。例如,如果isEquipped==true,则在之后写入打印(“isEquipped为true”)
。如果在每个If语句之后执行此操作,您将能够看到代码停止的位置。如果你试试这个会怎么样?
Mouse.Button1Down:connect(function()
if isEquipped == true then
if not pause then
pause = true
anim1:Play()
wait(0.1)
trail.Enabled = true
wait(0.6)
trail.Enabled = false
pause = false
end
end
end)
handle.Touched:connect(function(hit)
if isEquipped == true then
if not pause2 then
pause2 = true
if Mouse.Button1Down then
if humanoid and humanoid.Health > 0 and hit and not hit:isDescendantOf(person) then
local target = hit.Parent:FindFirstChild("Humanoid")
if target and target.Health > 0 then
target:TakeDamage(damage)
wait(0.7)
pause2 = false
end
end
end
end
end
end)
game.Players.LocalPlayer:GetMouse().KeyDown:Connect(function(KeyPressed)
if KeyPressed == "e" then
if not pause3 then
pause3 = true
if toggle == false then
toggle = true
isEquipped.Value = true
fake1.Transparency = 1
fake2.Transparency = 1
disc1.Transparency = 0
disc2.Transparency = 0
wait(0.7)
pause3 = false
else
pause3 = true
toggle = false
isEquipped.Value = false
fake1.Transparency = 0
fake2.Transparency = 0
disc1.Transparency = 1
disc2.Transparency = 1
wait(0.7)
pause3 = false
end
end
end
end)