Roblox 制作功能运行不止一次
Posted
技术标签:
【中文标题】Roblox 制作功能运行不止一次【英文标题】:Roblox making functions run more than once 【发布时间】:2018-09-14 19:25:03 【问题描述】:我的代码:
local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Activation =
Instance.new("Sound",game.Players.LocalPlayer.Character.Head)
local char = Player.Character
local hum = char.Humanoid
local root = char.HumanoidRootPart
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.F then
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://1581972610"
local animTrack = hum:LoadAnimation(animation)
animTrack:Play()
Activation.SoundId = "rbxassetid://1581091676" --Plays Mangekyou Sharingan Activation Sound.
Activation:Play()
wait(0.3)
game.Players.LocalPlayer.Character.Head.face.Texture = "rbxassetid://76285632" --When F is pressed, face texture changes to sharingan decal.
game:GetService("Chat"):Chat(Player.Character.Head, "Mangekyou Sharingan!")
end
end)
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.R then
Activation.SoundId = "rbxassetid://1580990602" --Plays Amaterasu Activation Sound.
Activation:Play()
game:GetService("Chat"):Chat(Player.Character.Head, "Amaterasu!")
local Target = Instance.new("Part") --makes a part
Target.CFrame = Mouse.Hit; --Makes part spawn at the mouse's current location in game
Target.Parent = game.Workspace
Target.Transparency = 1
Target.Anchored = true
Target.CanCollide = false
local Amaterasu = Instance.new("Fire")
Amaterasu.Parent = game.Workspace.Part
Amaterasu.Color = Color3.new(0,0,0)
Amaterasu.SecondaryColor = Color3.new(0,0,0) --amaterasu properties
Amaterasu.Size = 25
local R = Instance.new("RocketPropulsion") --rocket propulsion, parents amaterasu
R.Parent = Amaterasu
R.MaxThrust = 300
R.ThrustP = 30
R:Fire()
end
end)
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.G then
game.Players.LocalPlayer.Character.Head.face.Texture = "rbxassetid://22557247" --When G is pressed, face texture changes back to normal.(leaves face blank isnt working :/)
end
end)
我正在处理这个脚本中的第二个函数,如果按下“r”键就会激活该函数。该功能通过按“r”键使部分产生到鼠标当前位置,其中包含火焰。
这一切正常,除了第一次按“r”在鼠标位置生成零件后,如果我将鼠标的位置移动到另一个区域并再次按“r”,它会重复函数中的所有内容,但不会t 更改到新位置。
【问题讨论】:
你的意思是这行`Target.CFrame = Mouse.Hit`导致相同的位置吗?好吧,您必须检查此处分配的内容。可能是Mouse
实例每次都需要更新。
【参考方案1】:
您应该尝试在按下 R 键时运行的函数中运行 Mouse = Player:GetMouse()
以更新鼠标的位置。您更新后的活动如下所示:
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.R then
Mouse = Player:GetMouse() --This line updates the mouse and presumably, its location.
Activation.SoundId = "rbxassetid://1580990602"Sound.
Activation:Play()
game:GetService("Chat"):Chat(Player.Character.Head, "Amaterasu!")
local Target = Instance.new("Part")
Target.CFrame = Mouse.Hit;
--All the other stuff you're doing goes here
这样,鼠标的位置会在每次输入 if 块时更新(即每次用户按 R 键时)。
【讨论】:
以上是关于Roblox 制作功能运行不止一次的主要内容,如果未能解决你的问题,请参考以下文章