Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

2-D Ball Bouncing Won't Work?

##Problem So basically I'm working on a project where I am attempting to simulate somewhat realistic physics with this ball.

I have a main script that calculates everything and makes everything interact. This is where the problem begins. Almost everything is working fine so far except for the bouncing. I made a function that should make the ball appear to 'bounce' when it 'hits' the ground.

In Studio, this works perfectly fine. I've recorded and uploaded a video of that here

In game however, it seems to glitch out when it makes impact, and the function fires several times. You can see that here. (I have only added in function for top-bottom collisions, so don't worry about glitching through the sides)

Just so you know, I already did attempt making a debounce within the function, but it doesn't seem to affect it, and i'm not sure why. Also, this is in a LocalScript.


##Script (Remember, these are both portions of the same script)

Here's the bouncing function:

function Impact()
	if DownForce.Value>128 and Bouncing==false then
		Bouncing=true
		DownForce.Value=-(DownForce.Value/1.75)
		Ball.Size=UDim2.new(0,11,0,9)
		wait()
		Ball.Size=UDim2.new(0,11,0,8)
		wait()
		Ball.Size=UDim2.new(0,12,0,7)
		wait()
		Ball.Size=UDim2.new(0,12,0,6)
		wait()
		Ball.Size=UDim2.new(0,11,0,7)
		wait()
		Ball.Size=UDim2.new(0,11,0,8)
		wait()
		Ball.Size=UDim2.new(0,10,0,9)
		wait()
		Ball.Size=UDim2.new(0,10,0,10)
		Bouncing=false
	end
end

Now this is the script that fires the function for when the ball makes impact:

function Check()
	local TopCollide=false
	local BottomCollide=false
	local LeftCollide=false
	local RightCollide=false
	for _,Object in pairs(Objects) do
		local Top=Object.AbsolutePosition.Y
		local Bottom=Top+Object.AbsoluteSize.Y
		local Left=Object.AbsolutePosition.X
		local Right=Left+Object.AbsoluteSize.X
		--[^]Object]-[Ball]v]--
		local BTop=Ball.AbsolutePosition.Y
		local BBottom=BTop+Ball.AbsoluteSize.Y
		local BLeft=Ball.AbsolutePosition.X
		local BRight=BLeft+Ball.AbsoluteSize.X
		
		if Top-Ball.AbsoluteSize.Y<=BTop and Left-Ball.AbsoluteSize.X<=BLeft and Right+Ball.AbsoluteSize.X>=BRight and Bottom>=BTop and DownForce.Value>=0 then
			BottomCollide=true
			StopY=Top-Ball.AbsoluteSize.Y
		end
		
	end
	
	if BottomCollide==true then
		Impact()
		BBlocked.Value=true
	else
		BBlocked.Value=false
	end
	
	if StopY and not BBlocked and not TBlocked then
		StopY=nil
	end
end

Anyways, I apologize for the long bits of code, I just have no idea where to look for the problem. I know that it's somewhere within this however, and I would really appreciate it if anyone could help me out. Thanks in advance!

1 answer

Try running the jump function without the condition

This discussion is preserved as it was. New answers, comments, and votes are closed.