游戏未检测到触摸事件



我正试图为我的NPC制作一个脚本,该脚本将在触摸NPC手中手套的玩家体内放置一个身体速度,但它没有起到任何作用。我试着制作一个触摸事件,当有东西触摸手套时,它会打印出一些东西,但也不起作用。

代码:

local cooldown = 0.7
activated = false
deb = false
slapped = false
local slapanim = script.Parent.Parent.Humanoid:LoadAnimation(script.Parent.SlapAnim)



while true do
if activated == false and deb == false then
deb = true
activated = true
slapanim:Play()
wait(0.3)
activated = false
wait(cooldown-0.3)
slapped = false
deb = false
end
end

--[[if activated == true and slapped == false and hit.Parent:FindFirstChild("Humanoid") and hit.Parent ~= script.Parent.Parent then
print('b')
slapped = true
script.Parent.Hand.Slap:Play()  
hit.Parent.Humanoid.Sit = true
local velocity = Instance.new('BodyVelocity', hit.Parent.HumanoidRootPart)
velocity.MaxForce = Vector3.new(3,3,3) *math.huge
local dir = (hit.Parent.HumanoidRootPart.CFrame.Position - script.Parent.Parent.HumanoidRootPart.CFrame.Position)
velocity.Velocity = (dir +Vector3.new(0,2,0)).Unit *25
local rot= Instance.new('BodyAngularVelocity', hit.Parent.HumanoidRootPart)
rot.AngularVelocity = Vector3.new(1,1,1) *math.pi *2
rot.MaxTorque = Vector3.new(2,2,2) *math.huge
rot.P = 5000

game:GetService('Debris'):AddItem(velocity, 0.3)
game:GetService('Debris'):AddItem(rot, 0.3)
wait(0.3)
hit.Parent.Humanoid.Sit = false
end]]
script.Parent.Hand.Touched:Connect(function(hit)
print(hit.Parent.Name)
end)

您的代码不工作的原因是,无论何时activated和deb都为true,您的循环都会因脚本过度执行而自行停止。

只需在代码中添加task.wait()即可修复此问题

while true do
task.wait()
if activated == false and deb == false then
deb = true
activated = true
slapanim:Play()
wait(0.3)
activated = false
wait(cooldown-0.3)
slapped = false
deb = false
end
end

最新更新