How can i use a model as a tool for the player that when click LMB it triggers a animation and flings the character a certain distance if made contact with?
how to use a model as a tool to fling people? (im new to lua)
1 answer
Hello,
Read Time ~ 15 mins
The topic of using a Tool to fling some one by RayCasting might be complicated but I will try my best to explain.
Creating the Tool
Create A Tool in starter Pack and name it whatever you want, Maybe "Fling Tool". Insert a Part in the Tool and Name it "Handle" or else it wont work. if you have more than 1 part in your tool, you may have to weld the extra parts to the "Handle". Under "Handle" Create a folder. Name the Folder "Code" or something else to keep your Explorer organized. Add the Instances below under the folder,
Script
Local Script
RemoteEvent
Local Script
If You have followed the steps above correctly, you can reference to the parts in the tool perfectly, Define your "Tool" Variable
Code Example, Defining Tool
local Tool = script.Parent.Parent.Parent <---
Next, Define the Remote Event Variable
Code Example, Defining RemoteEvent
local Tool = script.Parent.Parent.Parent
local Event = script.Parent:WaitForChild("RemoteEvent") <---
After that, Define a Variable for Mouse
Code Example, Defining Mouse
local Tool = script.Parent.Parent.Parent
local Event = script.Parent:WaitForChild("RemoteEvent")
local Mouse = game.Players.LocalPlayer:GetMouse() <---
Then, We will connect function to a Tool Activated Event
Code Example, Connect event to Tool.Activated
local Tool = script.Parent.Parent.Parent
local Event = script.Parent:WaitForChild("RemoteEvent")
local Mouse = game.Players.LocalPlayer:GetMouse()
Tool.Activated:Connect(function() <---
end) <---
Now we will Define the Mouse Hit Position in the 3D world, this will be for the Ray Casting for later
Code Example, Reference to Mouse.Hit.Position
local Tool = script.Parent.Parent.Parent
local Event = script.Parent:WaitForChild("RemoteEvent")
local Mouse = game.Players.LocalPlayer:GetMouse()
Tool.Activated:Connect(function()
local MouseVector = Mouse.Hit.Position <---
end)
After All, Fire the Remote Event to server with a MousePosition Parameter
Code Example, Firing Remote Event to Server
local Tool = script.Parent.Parent.Parent
local Event = script.Parent:WaitForChild("RemoteEvent")
local Mouse = game.Players.LocalPlayer:GetMouse()
Tool.Activated:Connect(function()
local MouseVector = Mouse.Hit.Position
Event:FireServer(MouseVector) <---
end)
Congrats, Your Local/Client script is complete! Next will be the Server-Script
Script
First Of All, Define your Remote Event
Code Example, Define Remote-Event
local Event = script.Parent.RemoteEvent <---
Define the Tool for RayCast.
Code Example, Defining Tool
local Event = script.Parent.RemoteEvent
local Tool = script.Parent.Parent.Parent <---
Then Connect function to a OnServerEvent Event and define the two Parameters of Player, and MouseVector
Code Example, Connecting Function to OnServerEvent
local Event = script.Parent.RemoteEvent
local Tool = script.Parent.Parent.Parent
Event.OnServerEvent:Connect(function(Player, MousePosition) <---
end) <---
Define RayCast Params which will be our third Parameter for the RayCast. Make RayCast Parameters in which we will be blacklisting some parts Including Players Character.
Code Example, RayCast Parameters
local Event = script.Parent.RemoteEvent
local Tool = script.Parent.Parent.Parent
Event.OnServerEvent:Connect(function(Player, MousePosition)
local RayParams = RaycastParams.new() <---
RayParams.FilterDescendantsInstances = {Player.Character} <---
RayParams.FilterType = Enum.RaycastFilterType.Blacklist <---
end)
Now we will Define the Origin Position in Vector3, which will be the handle position.
Code Example, Defining Handle Position
local Event = script.Parent.RemoteEvent
local Tool = script.Parent.Parent.Parent
Event.OnServerEvent:Connect(function(Player, MousePosition)
local RayParams = RaycastParams.new()
RayParams.FilterDescendantsInstances = {Player.Character}
RayParams.FilterType = Enum.RaycastFilterType.Blacklist
local Origin = Tool.Handle.Position <---
end)
We will now Extend the Ray with some math, Setting limits is our best option.
Code Example, Extending Ray
local Event = script.Parent.RemoteEvent
local Tool = script.Parent.Parent.Parent
local Limit = 1000 <---
Event.OnServerEvent:Connect(function(Player, MousePosition)
local RayParams = RaycastParams.new()
RayParams.FilterDescendantsInstances = {Player.Character}
RayParams.FilterType = Enum.RaycastFilterType.Blacklist
local Origin = Tool.Handle.Position
local LimitToRay = Origin + (MousePosition - Origin).Unit * Limit <---
end)
Casting A Ray
Ray will have 3 parameters, First RayOrigin, Second RayDirection, Third RayParameters
Code Example, Casting A Ray in workspace
local Event = script.Parent.RemoteEvent
local Tool = script.Parent.Parent.Parent
local Limit = 1000
Event.OnServerEvent:Connect(function(Player, MousePosition)
local RayParams = RaycastParams.new()
RayParams.FilterDescendantsInstances = {Player.Character}
RayParams.FilterType = Enum.RaycastFilterType.Blacklist
local Origin = Tool.Handle.Position
local LimitToRay = Origin + (MousePosition - Origin).Unit * Limit
local RayCast = workspace:Raycast(Origin, LimitToRay, RayParams) <---
end)
To get Player from the Ray first we will check if the Ray hit Something. if there is a Humanoid in Hit.Parent then we will Disable Humanoid.PlatformStand
Code Example, Getting Humanoid
local Event = script.Parent.RemoteEvent
local Tool = script.Parent.Parent.Parent
local Limit = 1000
Event.OnServerEvent:Connect(function(Player, MousePosition)
local RayParams = RaycastParams.new()
RayParams.FilterDescendantsInstances = {Player.Character}
RayParams.FilterType = Enum.RaycastFilterType.Blacklist
local Origin = Tool.Handle.Position
local LimitToRay = Origin + (MousePosition - Origin).Unit * Limit
local RayCast = workspace:Raycast(Origin, LimitToRay, RayParams)
if RayCast then <---
if RayCast.Instance.Parent:FindFirstChild("Humanoid") then <---
RayCast.Instance.Parent:FindFirstChild("Humanoid").PlatformStand = true <---
end <---
end <---
end) <---
After Enabled the Platform stand we will apply a force, the force will be random to get more fun from it
Code Example, Flinging Player
local Event = script.Parent.RemoteEvent
local Tool = script.Parent.Parent.Parent
local Limit = 1000
Event.OnServerEvent:Connect(function(Player, MousePosition)
local RayParams = RaycastParams.new()
RayParams.FilterDescendantsInstances = {Player.Character}
RayParams.FilterType = Enum.RaycastFilterType.Blacklist
local Origin = Tool.Handle.Position
local LimitToRay = Origin + (MousePosition - Origin).Unit * Limit
local RayCast = workspace:Raycast(Origin, LimitToRay, RayParams)
if RayCast then
if RayCast.Instance.Parent:FindFirstChild("Humanoid") then
RayCast.Instance.Parent:FindFirstChild("Humanoid").PlatformStand = true
RayCast.Instance.Parent.PrimaryPart.AssemblyAngularVelocity = Vector3.new(math.random(99,999),math.random(99,999),math.random(99,999)) <--- Fling
end
end
end)
Final Scripts
local Script
local Tool = script.Parent.Parent.Parent -- Tool
local Event = script.Parent:WaitForChild("RemoteEvent") -- Wait for Remote
local Mouse = game.Players.LocalPlayer:GetMouse() -- Player Mouse
Tool.Activated:Connect(function()
local MouseVector = Mouse.Hit.Position
Event:FireServer(MouseVector) -- Fire Server with Mouse Position
end)
Server-Script
local Event = script.Parent.RemoteEvent
local Tool = script.Parent.Parent.Parent
local Limit = 1000
Event.OnServerEvent:Connect(function(Player, MousePosition)
local RayParams = RaycastParams.new() -- Filtering Character
RayParams.FilterDescendantsInstances = {Player.Character}
RayParams.FilterType = Enum.RaycastFilterType.Blacklist
local Origin = Tool.Handle.Position -- Tool Position
local LimitToRay = Origin + (MousePosition - Origin).Unit * Limit -- Applies Limit
local RayCast = workspace:Raycast(Origin, LimitToRay, RayParams) -- Fire Ray
if RayCast then
if RayCast.Instance.Parent:FindFirstChild("Humanoid") then
RayCast.Instance.Parent:FindFirstChild("Humanoid").PlatformStand = true
RayCast.Instance.Parent.PrimaryPart.AssemblyAngularVelocity = Vector3.new(math.random(99,999),math.random(99,999),math.random(99,999)) -- Apply force
end
end
end)
More On RayCasting:
Intro To RayCasting - By Roblox
Final Working Model:
This discussion is preserved as it was. New answers, comments, and votes are closed.

5 comments
hmm.. Can you still use platform stand to ragdoll a character and then apply a force to fling the character?
That might be easier than abusing collision physics.
Could you define platform standing?
https://developer.roblox.com/en-us/api-reference/property/Humanoid/PlatformStand
have you played slap battles before? if yes then I kind of wanna mimic that that when clicked it if it makes contact with another player they get flinged