Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

My part does not move to mouse position?

Asked by 1 month ago
Edited 1 month ago
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.

0
learn roblox magnitude i think it will help Zeuxulaz 145 — 1mo
0
OK, thanks NathanBlox_Studios 113 — 1mo
0
Roblox does not recommend that you use mouse.KeyDown. Use UserInputService instead. Dovydas1118 993 — 1mo
0
It has been working well with all my scripts, but OK then NathanBlox_Studios 113 — 1mo
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 Dovydas1118 993 — 1mo

1 answer

Log in to vote
0
Answered by 1 month ago
Edited 1 month ago

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)
Ad

Answer this question