[SCROLL DOWN TO THE LATEST REPLY I DIDNT WANT TO CLOG POSTS]
I’ve been working on this game for a little bit where you play soccer, I’ve encountered a problem with heartbeat where it’s slower in game than in studio. I’ve tried a few solutions but none seem to work out.
In-Game:
Studio:
(The heartbeat rate is ~240)
I don’t want a whole system being written, I just need to know where I should look for ways to make this possible, alongside with a prediction system.
My code currently:
game["Run Service"].Heartbeat:Connect(function(dt)
BallPhysicsEnabled = not (BallHolder)
Ball.Trail.Enabled = BallPhysicsEnabled
if BallPhysicsEnabled then
local vel:Vector3 = Ball:GetAttribute("Velocity")
local velWithoutY = Vector3.new(vel.X,0,vel.Z)
local params = RaycastParams.new(); params.FilterType = Enum.RaycastFilterType.Include; params.FilterDescendantsInstances = {workspace.Field}
local CheckResolution = Ball.Size.Y/16
local AirTime = tick()-AirBegin
local angVel:Vector3 = Ball:GetAttribute("AngularVelocity")
local rayDir = vel*Ball.Size.Y
local movementRay = workspace:Raycast(Ball.Position,rayDir,params)
if movementRay then
local normal =movementRay.Normal
if normal == Vector3.new(0,1,0) then
vel = Vector3.new(vel.X,-vel.Y*BounceAmount,vel.Z)
else
vel *= -normal
end
func:PlaySound(SoundService.SFX.BallGround,.5,Ball)
end
velWithoutY = Vector3.new(vel.X,0,vel.Z)
local gRay = workspace:Raycast(Ball.Position,Vector3.new(0,-5000,0),params); local dist; if gRay then dist = gRay.Distance GroundPosition=gRay.Position.Y else dist = math.huge end
Grounded = dist <= CheckResolution+Ball.Size.Y/1.85
if not Grounded then
vel -= Vector3.new(0,GravityAmount,0)
vel = Vector3.new(vel.X,math.clamp(vel.Y,-CheckResolution*16,math.huge),vel.Z)
if not OldGrounded then AirBegin = tick() end
else
if OldGrounded and vel.Y < 0 and math.abs(vel.Y) <= 0.1 then
vel = velWithoutY
func:PlaySound(SoundService.SFX.BallGround,.5,Ball)
end
end
if vel.Magnitude > 0.1 then
angVel += Ball.CFrame:VectorToWorldSpace(Vector3.new(vel.Unit.Z,0,0))
end
local frictionAmount = .005; if Grounded then frictionAmount = .015 end
Ball.Rotation += angVel/18; angVel = angVel:Lerp(Vector3.zero,frictionAmount)
local bpos = Ball.Position+vel
local grnd = workspace.Field.MainGround
local x = math.clamp(bpos.X,-grnd.Size.X/2,grnd.Size.X/2)
local z = math.clamp(bpos.Z,-grnd.Size.Z/2,grnd.Size.Z/2)
Ball.Position = Vector3.new(x,math.clamp(bpos.Y,GroundPosition+CheckResolution,math.huge),z)
Ball:SetAttribute("Velocity",velWithoutY:Lerp(Vector3.zero,frictionAmount)+Vector3.new(0,vel.Y,0))
Ball:SetAttribute("AngularVelocity",angVel)
OldGrounded = Grounded
if CanScore then
local touching = workspace:GetPartsInPart(Ball)
for i,v in touching do
if v.Parent == workspace.GoalHitboxes then
ScoreAgainst(v.Name,LastTouch.Player)
end
end
end
else
Ball:SetAttribute("Velocity",Vector3.zero)
Ball:SetAttribute("AngularVelocity",Vector3.zero)
end
if BallHolder then
Ball.Position = BallHolder.HumanoidRootPart.BallPosition.WorldPosition
end
end)