Roblox making functions run more than once

我的未来我决定 提交于 2020-01-03 05:45:27

问题


My code:

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)

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.

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.


回答1:


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

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



来源:https://stackoverflow.com/questions/49664450/roblox-making-functions-run-more-than-once

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!