Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

My part does not move to mouse position?

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.

5 comments

0

learn roblox magnitude i think it will help

0

OK, thanks

0

Roblox does not recommend that you use mouse.KeyDown. Use UserInputService instead.

0

It has been working well with all my scripts, but OK then

0

I believe you can use mouse.Hit in order  to move to an object in 3D Space. Also if you're trying to make from point A to point B, then use your starting position and mouse.Hit

1 answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.