由于某种原因,我的模块脚本中的局部变量似乎都是nil,没有确切的解释或解决方案。 本地脚本(耐力条脚本):
local ms = require(game.ReplicatedStorage.ModuleScript)
if not ms then
local ms = require(game.ReplicatedStorage:WaitForChild("ModuleScript"))
end
local TW = game:GetService("TweenService")--Get Tween Service
local Staminabar = script.Parent -- Get The Stamina bar
local function UpdateStaminabar() --Stamina Bar Size Change Function
local stamina = math.clamp(ms.stamina / ms.maxstamina, 0, 1) --Maths
local info = TweenInfo.new(ms.stamina / ms.maxstamina,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut,0,false,0) --Tween Info
TW:Create(script.Parent,info,{Size = UDim2.fromScale(ms.stamina, 1)}):Play() -- Create The Tween Then Play It
end
UpdateStaminabar()--Update The Health Bar
ms.stamina:GetPropertyChangedSignal("Value"):Connect(UpdateStaminabar) --Update The Health Bar When The Health Change
ms.maxstamina:GetPropertyChangedSignal("Value"):Connect(UpdateStaminabar) --Update The Health Bar Wheb The MaxHealth Change
LocalScript(战斗脚本):
local ms = require(game.ReplicatedStorage.ModuleScript)
if not ms then
local ms = require(game.ReplicatedStorage:WaitForChild("ModuleScript"))
end
local uis = game:GetService("UserInputService")
local cas = game:GetService("ContextActionService")
local rs = game:GetService("ReplicatedStorage")
local events = rs:WaitForChild("Events")
local hitbox = events:WaitForChild("Hitbox")
local plr = game.Players.LocalPlayer
local character = plr.Character
if not character then
plr:WaitForChild("Character")
end
local humanoid = character.Humanoid
local animator = humanoid:WaitForChild("Animator")
local idle = animator:LoadAnimation(script:WaitForChild("idle"))
local jab = animator:LoadAnimation(script:WaitForChild("jab"))
local rightcross = animator:LoadAnimation(script:WaitForChild("rightstraight"))
local lefthook = animator:LoadAnimation(script:WaitForChild("lefthook"))
local righthook = animator:LoadAnimation(script:WaitForChild("righthook"))
local swingsfx = script:WaitForChild("Air swing")
local currentPunch = 0
local currentPunch2 = 0
local debounce1 = false
local debounce2 = false
local function onInputBegan(input)
if debounce2 == true then return end
if ms.stamina <= 0 then return end
if input.KeyCode == Enum.KeyCode.Q then
ms.stamina -= 20
debounce2 = true
humanoid.WalkSpeed = 0.5
lefthook:Play()
swingsfx:Play()
hitbox:FireServer(Vector3.new(4, 5, 3), Vector3.new(4.5, 1), 7.5, 0.15)
task.wait(0.5)
humanoid.WalkSpeed = 10
lefthook:Stop()
task.wait(6)
debounce2 = false
end
if input.KeyCode == Enum.KeyCode.E then
ms.stamina -= 20
debounce2 = true
humanoid.WalkSpeed = 0.5
righthook:Play()
swingsfx:Play()
hitbox:FireServer(Vector3.new(4, 5, 3), Vector3.new(4.5, 1), 7.5, 0.15)
task.wait(0.5)
humanoid.WalkSpeed = 10
righthook:Stop()
task.wait(6)
debounce2 = false
end
end
local function punch()
if debounce1 then return end
debounce1 = true
if currentPunch == 0 then
ms.stamina -= 10
humanoid.WalkSpeed = 0.6
jab:Play()
swingsfx:Play()
hitbox:FireServer(Vector3.new(4, 5, 3), Vector3.new(5.7, 1), 2.5, 0.15)
task.wait(0.5)
humanoid.WalkSpeed = 10
jab:Stop()
debounce1 = false
elseif currentPunch == 1 then
ms.stamina -= 10
humanoid.WalkSpeed = 0.6
rightcross:Play()
swingsfx:Play()
hitbox:FireServer(Vector3.new(4, 5, 3), Vector3.new(5.7, 1), 2.5, 0.15)
task.wait(0.5)
humanoid.WalkSpeed = 10
rightcross:Stop()
debounce1 = false
elseif currentPunch == 2 then
ms.stamina -= 10
humanoid.WalkSpeed = 0.6
jab:Play()
swingsfx:Play()
hitbox:FireServer(Vector3.new(4, 5, 3), Vector3.new(5.7, 1), 2.5, 0.15)
task.wait(0.5)
humanoid.WalkSpeed = 10
jab:Stop()
debounce1 = false
elseif currentPunch == 3 then
ms.stamina -= 10
debounce2 = true
humanoid.WalkSpeed = 0.6
rightcross:Play()
swingsfx:Play()
hitbox:FireServer(Vector3.new(4, 5, 3), Vector3.new(5.7, 1), 5, 0.15)
task.wait(0.5)
humanoid.WalkSpeed = 10
rightcross:Stop()
debounce1 = false
debounce2 = false
end
if currentPunch == 3 then
currentPunch = 0
debounce1 = true
wait(2)
debounce1 = false
else
currentPunch += 1
end
end
cas:BindAction("Punch", punch, true, Enum.UserInputType.MouseButton1)
uis.InputBegan:Connect(onInputBegan)
模块脚本:
local info = {}
local stamina = 100
local maxstamina = 100
return info
我试图制作一个耐力系统,以及一个耐力条,但他们只是拒绝使用当地人,而是打印出如下错误:
15:00:30.915 Players.Morelval1.PlayerGui.ScreenGui.StaminaBar.Frame.StaminaBar 脚本:12:尝试对 nil 执行算术(div) - 客户端 - StaminaBar 脚本:12 15:00:32.505 Players.Morelval1.PlayerGui.Main.PunchScript:39:尝试比较 nil <= number - Client - PunchScript:39
我期待变量能够交互并被使用。
local ms = require(game.ReplicatedStorage.ModuleScript)
if not ms then
local ms = require(game.ReplicatedStorage:WaitForChild("ModuleScript"))
end
第二个
ms
是 if 语句的局部变量。它遮盖了第一个ms
。因此,如果第一个 require 失败,您会将第二个 require 的返回值存储到错误的变量中。
删除 local 关键字。
local ms = require(game.ReplicatedStorage.ModuleScript)
if not ms then
ms = require(game.ReplicatedStorage:WaitForChild("ModuleScript"))
end
或者简单地
local ms = require(game.ReplicatedStorage.ModuleScript)
or require(game.ReplicatedStorage:WaitForChild("ModuleScript"))
当然,在继续之前,您仍然应该检查第二次 require 尝试是否成功。