I am trying to make a ball bounce off a block. I cannot get it to work though, I’ve tried it with BodyVelocity and BodyForce, I’ve tried it with Instance.new() and even with them already there.
Please let me know if you can find the issue.
This is my code:
if otherPart.Name == "Bouncer" then
local move = Instance.new('BodyVelocity')
move.MaxForce = Vector3.new(1,1,1) * math.huge
move.Velocity = script.Parent.CFrame.upVector * 100
end
end
It looks like the problem is that you’re not parenting the new BodyVelocity to the part you want to move. Being parented to the part is how the Body movers work. Don’t forget about the new constraints system though, they can work as well or even better!
if otherPart.Name == "Bouncer" then
local move = Instance.new('BodyVelocity')
move.Parent = script.Parent
move.MaxForce = Vector3.new(1,1,1) * math.huge
move.Velocity = script.Parent.CFrame.upVector * 100
end
end
if otherPart.Name == "Bouncer" then
local move = Instance.new('BodyVelocity', otherPart) -- The parent is just a guess from the context of your code, feel free to change it!
move.MaxForce = Vector3.new(1,1,1) * math.huge
move.Velocity = script.Parent.CFrame.upVector * 100
end
Typically you want to parent last when adjusting your parts, and not in the Instance.new function itself. So
if otherPart.Name == "Bouncer" then
local move = Instance.new('BodyVelocity')
move.MaxForce = Vector3.new(1,1,1) * math.huge
move.Velocity = script.Parent.CFrame.upVector * 100
move.Parent = script.Parent
end
The reason for this isn’t something I can remember off the top of my head, but I read that it’s faster for the game to process, and it’s basically to no cost for the programmer’s time, so its good practice to do it this way.
Normal scripts run on the server. Local scripts run on the client. Putting it in a local script for the client would work but it would only show on the client and not replicate to the server and other clients, and the movement of it would only replicate if the client is the NetworkOwner (the client/server the simulates the physics for a part). It’s up to you what you use for your needs, but most likely it would be on the server. Either way it should show up when you press play and spawn in regardless.
I am incredibly sceptical if this is a good way to make things bounce. I would recommend using VectorForces instead or even just use the default Custom Physical Properties and change the density and elasticity or the ball.
Maybe I should have mention this in the beginning but its for a ping pong ball for ping pong that can be played with an “AI” which is basically just a wall that the ball bounces off of, I’ve just tested this but it does not seem to have enough momentum to keep a game going.
The spawn that is at the bottom has elasticityWeight set to 0 and the Ball elasticityWeight is 1.That is to make sure the ball is bouncing off the ground and not the other way around.
I set the elasticy of the ball to 1. I set the density to 10 so that it will be affected less if something else hits it but you don’t need to…
In theory the ball will bouncy forever however due to floating point inaccuracies it might last for a couple of days-weeks or so. Maybe more maybe less that’s just a guess.