I made a script when a player touches a part it flings away from them. I am wanting to know how to add a feature to my script, when the part hits another part it will deflect back off the part.
I already have a part that I want for the object and after looking at the scripts I don’t know how to remove the makings of the fireball and also combining my script and the fireball scripts together.
Also in the post it has a script, a local script, and a remotevent. Is there also a way to do this with out all of that and just make it in one script, or is it better to do it that way?
How do I accomplish this?
This script just makes the part fling in the direction the player is facing.
local part = script.Parent
local TweenService = game:GetService("TweenService")
local function onTouch(hit)
local character = hit.Parent
local tool = character:FindFirstChildOfClass("Tool")
if tool and tool:FindFirstChild("Handle") then
local direction = character:GetPrimaryPartCFrame().lookVector
local distance = 50 -- Adjust the distance as needed
local duration = 1 -- Adjust the duration of the tween as needed
local endPosition = part.Position + direction * distance
local tweenInfo = TweenInfo.new(duration)
local goal = {}
goal.Position = endPosition
local tween = TweenService:Create(part, tweenInfo, goal)
tween:Play()
end
end
part.Touched:Connect(onTouch)
Instead of using TweenService, you can simply change the velocity to let physics do the work. Plus, it will be less buggy. To do this, you can follow this very simple code and mess around with it:
script.Parent.Touched:Connect(function(part)
if not part.Parent:FindFirstChild('Humanoid') then return end
part.Velocity = Vector3.new(10, 100, 50)
end)
The second line will check if it is a player or not. You can remove this if you’d like.
Let me know if you have any questions or concerns!
I am trying to get it where when it touches a player it will go in the direction the player is facing. What the codes does is that but I need it to go forward and not back to where the player is facing. What do I need to change to make this happen?
--SERVER SCRIPT LOACTED IN PART
-- <<VERIABLES>>
local part = script.Parent
-- <<FUNCTIONS>>
-- Function to handle part's movement
local function onTouch(hit)
if not hit.Parent:FindFirstChild('Humanoid') then return end
local direction = hit.Parent:GetPrimaryPartCFrame().lookVector
part.Velocity = Vector3.new(50, direction.y, direction.x)
end
-- <<CONNECTIONS>>
part.Touched:Connect(onTouch)
I got to got in the direction of the player but it only happens once
-- get inversed rotation
local inversedCFrame = part.CFrame:Inverse().Rotation + part.Position
local direction = inversedCFrame.LookVector
-- then you can use TS or velocity to apply move by direction
Also you can replace deprecated :GetPrimaryPartCFrame() with character.PrimaryPart.CFrame
--SERVER SCRIPT LOCATED IN PART
-- <<VARIABLES>>
local part = script.Parent
-- <<FUNCTIONS>>
-- Function to handle part's movement
local function onTouch(hit)
if not hit.Parent:FindFirstChild('Humanoid') then return end
local direction = hit.Parent:GetPrimaryPartCFrame().lookVector
part.Velocity = Vector3.new(50 * direction.x, 50 * direction.y, 50 * direction.z)
end
-- <<CONNECTIONS>>
part.Touched:Connect(onTouch)
I made it some what to hover but when I hit play the ball goes fling around. (The part is inside a cage so it can’t fling out) How do I make that not happen but keep the part moving in the direction of the player and hover over the ground? I am wanting to lock the part on the y axis so it does not go up and down but I want it to be like 1/2 or 1 stud off the ground.
-- SERVER SCRIPT LOCATED IN PART
-- <<VARIABLES>>
local part = script.Parent
local hoverHeight = 2 -- Adjust this value to set the desired hover height
local hoverForce = Vector3.new(0, 10, 0) -- Adjust this value to control the strength of the hover force
-- <<FUNCTIONS>>
-- Function to handle part's movement
local function onTouch(hit)
if not hit.Parent:FindFirstChild('Humanoid') then return end
local direction = hit.Parent:GetPrimaryPartCFrame().lookVector
local horizontalDirection = Vector3.new(direction.x, 0, direction.z).Unit
part.Velocity = Vector3.new(50 * horizontalDirection.x, 0, 50 * horizontalDirection.z)
end
-- Function to apply hover force
local function applyHoverForce()
while wait(0.1) do
local distanceToHoverHeight = part.Position.y - hoverHeight
-- Apply hover force using BodyVelocity
local bodyVelocity = Instance.new("BodyVelocity")
bodyVelocity.Velocity = Vector3.new(0, hoverForce.y * distanceToHoverHeight, 0)
bodyVelocity.MaxForce = Vector3.new(0, math.huge, 0)
bodyVelocity.P = math.huge
bodyVelocity.Parent = part
wait(0.1) -- Adjust this delay based on your needs
bodyVelocity:Destroy()
end
end
-- <<CONNECTIONS>>
part.Touched:Connect(onTouch)
part.AncestryChanged:Connect(function()
if part.Parent then
applyHoverForce()
end
end)
-- Apply hover force initially
applyHoverForce()
Also if there is a better way to do this please let me know.