local player = game.Players.LocalPlayer local mouse = player:GetMouse() local humanoid = player.Character:FindFirstChild("Humanoid") local animate local par = humanoid.Parent:WaitForChild("LeftHand") local db = false local ball = game.ReplicatedStorage.Energy local function shootenergy() wait(0.03) local clone = ball:Clone() clone.Parent = par clone.Position = par.Position wait(0.2) ball.Position = Vector3.new(mouse.Hit.p) end mouse.KeyDown:Connect(function(key) if key == "c" then local animation = Instance.new("Animation", player.Character) animation.AnimationId = "rbxassetid://5482423295" animate = humanoid:LoadAnimation(animation) animate:Play() shootenergy() game.ReplicatedStorage.Event.AddEnergy:FireServer() db = true wait(1) db = false end end)
So basically, the script is supposed to make a ball move from the players hand to the mouse position in the 3d space. But it starts at the player and stops while the script stops from the calling of the function. Please help.
You can use Mouse.Hit
in order to get the ball to your mouse. Mouse.Hit
returns a CFrame and you can use it to set the position. A thing I would like to mention is that roblox does not recommend using Mouse.KeyDown
, so i'll edit your script to include UserInputService
. I added some extra stuff, and I'll be noting stuff in --comments
.
local player = game.Players.LocalPlayer local mouse = player:GetMouse() local UIS = game:GetService("UserInputService") --This is what you need to get. local humanoid = player.Character:FindFirstChild("Humanoid") local animate local par = humanoid.Parent:WaitForChild("LeftHand") local db = false local ball = game.ReplicatedStorage.Energy --[[ I think you should use this on the server, not on the client. local function shootenergy() wait(0.03) local clone = ball:Clone() clone.Parent = par clone.Position = par.Position wait(0.2) ball.Position = Vector3.new(mouse.Hit.p) end ]] UIS.InputBegan:Connect(function(input, GPE) --Input is self-explanatory, and GPE stands for Game Processed Event. if GPE then return end --If someone is e.g. chatting, then do nothing. This is one of the advantages of using UIS. if input.KeyCode == Enum.KeyCode.C then if not db then --If you're planning to add a debounce, then use an if statement. db = true local animation = Instance.new("Animation") animation.Parent = player.Character --I prefer you use this method, as it is faster. animation.AnimationId = "rbxassetid://5482423295" animate = humanoid:LoadAnimation(animation) --shootenergy() game.ReplicatedStorage.Event.AddEnergy:FireServer(mouse.Hit.Position) --I added an extra parameter so that you can use this position on the SERVER, not on the client. wait(1) db = false end end end)