Roblox使函数多次运行 [英] Roblox making functions run more than once

查看:171
本文介绍了Roblox使函数多次运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码:

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"键在鼠标内部产生火焰的情况下将其生成到鼠标的当前位置.

I am working on the second function in this script, the one that activates if the "r" key is pressed. The function makes a part spawn to the mouses current location with flames inside of it by pressing the "r" key.

这一切正常,除非在我第一次按"r"在鼠标位置生成零件之后,如果我将鼠标的位置移到另一个区域并再次按"r",它将重复执行功能中的所有操作,但不会切换到新位置.

This works all fine except after the first time I press "r" to spawn the part at my mouse location, if I move my mouse's location to another area and press "r" again it repeats everything in the function but doesn't change to the new location.

推荐答案

您应该尝试在函数中运行Mouse = Player:GetMouse(),如果他们按下R键,则该函数将运行,以更新鼠标的位置.您更新的事件如下:

You should try running Mouse = Player:GetMouse() inside your function that runs if they press the R key, to update the mouse's location. Your updated event would look like:

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时),鼠标的位置都会更新.

This way, the mouse's location is updated every time the if block is entered (which is every time the user presses R).

这篇关于Roblox使函数多次运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆