Tool Recipe – Explosion Maker
Tool Recipe – Explosion Maker
Problem
You want a player tool to spawn explosions when the tool is equipped and the player clicks in the game world.
Solution
This recipe must be integrated inside a Tool
.
- Create a
Tool
and make sure it contains a part named Handle. - Insert a
LocalScript
into the tool and paste this code into it:
-- Paste in a LocalScript (not a Script) local tool = script.Parent local remoteEvent = tool:WaitForChild("RemoteMouseEvent") local player = game.Players.LocalPlayer local mouse = player:GetMouse() local function onActivate() remoteEvent:FireServer(mouse.Hit) end tool.Activated:Connect(onActivate)
- Insert a
Script
into the tool and paste this code into it:
-- Paste in a Script (not a LocalScript) local tool = script.Parent -- Create new remote event and parent it to the tool local remoteEvent = Instance.new("RemoteEvent") remoteEvent.Parent = tool -- Rename it so that the local script can look for it remoteEvent.Name = "RemoteMouseEvent" -- Create a reference for the remote event connection local remoteEventConnection -- Function which listens for a remote event local function onRemoteMouseEvent(player, clickLocation) local explosion = Instance.new("Explosion") explosion.DestroyJointRadiusPercent = 0 -- Make the explosion non-deadly explosion.Position = clickLocation.p explosion.Parent = workspace end local function onEquip() -- When tool is equipped, connect the listener function to the remote event remoteEventConnection = remoteEvent.OnServerEvent:Connect(onRemoteMouseEvent) end local function onUnequip() -- When tool is unequipped, disconnect the remote event connection remoteEventConnection:Disconnect() end tool.Equipped:Connect(onEquip) tool.Unequipped:Connect(onUnequip)
- Place the tool inside the
StarterPack
folder. - Run your game, activate the tool, and click around the game world.
Discussion
The LocalScript
basically detects when the player clicks or touches the screen. It then sends a remote event to the server when the tool is activated.
The Script
connects or disconnects a remote event when the tool is equipped or unequipped. When the event fires (on mouse click), it creates a new explosion instance at the click location.
This script also makes the explosion non-deadly, but you’re free to change the value to make harmful explosions.