Throwing Rock System

Hey! I’m Icy a semi new developer here on Roblox. I am making a Rock system where you can run and pick up rocks to throw at other players. ( The rocks are like snowballs ). I have tried some things out but I don’t really understand the code and how to do it. That’s my main thing, I don’t really care if it looks good If I don’t understand how it was made yk. So how could I make a rock system? Any ideas? If so could you try to explain it into the best of detail… I would also want to rock to be thrown where the mouse is at, not just out of the HumanoidRootPart. I understand that I cant just ask for code but I was just looking for some basic ideas. Thanks!

1 Like

Without giving you code here is how it could work.

For simplicity I’ll assume all rocks are already placed beforehand by you and you don’t have a random rock spawning system in place.

To pick the rocks up, the rocks can either be tools, or you can just make them (mesh)parts and handle the BasePart.Touched event. Personally I’d go for the latter and whenever a player touches the rock, give the player a tool and put the touched part’s Transparency to 1 and set CanCollide to false. After a given amount of time you can set the Transparency back to 0 and CanCollide back to ture effectively “respawning” the rock.
To give the player a tool look at the codesample of the Backpack.

Now that the player has a rock in their inventory you want them to be able to throw it. You can do that with ApplyImpulse as shown here. But there are many ways to do that. Please keep in mind that you can only get the mouse position in a local script! But throwing the rock in a local script means that only the player throwing the rock would see it thrown. To fix that you would need to throw the rock from a (server) script. To do that you can use a RemoteEvent, have a look at Networking Events to understand them better.

Okay cool! Thanks… I do have a picking rock up system already in place, I’m using a Proximity Prompt to achieve it. Thanks for responding, I understand RemoteEvents and Ill check out Networking events as well! Though I will still ask, how would I get the mouse’s position and send that to the server? A code example could be nice.

Yes.

--client
local Mouse = Player:GetMouse()

--later when you want to send it
YourRemoteEvent:FireServer(Mouse.Hit.Position)
--You can also do Mouse.Target to get what part the mouse is pointing at
--server
YourRemoteEvent.OnServerEvent:Connect(function(Player, MousePosition)
-- your mouse position is ready
end)

Okay cool so what I would do is

–client
local Mouse = Player:GetMouse()

YourRemoteEvent:FireServer(Mouse.Hit.Position)

– Server

YourRemoteEvent.OnServerEvent:Connect(function(Player, MousePosition)

mouse.Button1Down:Connect(function()

    local part = Instance.new("Part")
part.Size = Vector3.new(1,1,1)
part.CFrame = character.HumanoidRootPart.CFrame * CFrame.new(0,3,0)
part.Parent = workspace
local direction = mouse.Hit.LookVector
local forceMultipliter = 100 * part:GetMass() 
part:ApplyImpulse(direction * forceMultipliter)

end)

That’s how I would get a throwable rock? And just detect if the Part got touched?

There are a few things missing. First, you need to detect when the player activates (or clicks) from the local script, not the server. Then, the direction variable should be calculated like local direction = (MousePosition - character.Head.Position).Unit (you might need to make it negative).

Yes, and then you can damage the player that touched it.

Okay ill give it a try soon! I’ll let you know if it worked out! Thanks :grinning: :grinning:

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.