As the title clearly suggests, I am making a basketball game in roblox so I have recently been trying to make my basketball part, named "B-Ball" bounce in a continuous motion. This can be seen here with this code:
local bball = script.Parent
local function setPlayerJumpPower(player)
local character = player.Character
if character then
local humanoid = character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.JumpHeight = 3
print("Jump Power has been set to 3 for player " .. player.Name)
end
end
end
local function weldBallToHand(player)
local character = player.Character
if not character then return end
local righthand = character:FindFirstChild("RightHand") or character:FindFirstChild("Right Arm")
if not righthand then
print("RightHand or Right Arm not found for player " .. player.Name)
return
end
local wc = Instance.new("WeldConstraint")
wc.Part0 = righthand
wc.Part1 = bball
wc.Parent = bball
bball.Position = righthand.Position + Vector3.new(0, 2, 0)
end
for _, player in pairs(game.Players:GetPlayers()) do
setPlayerJumpPower(player)
end
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
setPlayerJumpPower(player)
end)
end)
local function animateBallContinuously()
local initialPosition = bball.Position
local bounceHeight = 6
local bounceTime = 0.5
local tweenService = game:GetService("TweenService")
local upTweenInfo = TweenInfo.new(bounceTime, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local downTweenInfo = TweenInfo.new(bounceTime, Enum.EasingStyle.Quad, Enum.EasingDirection.In)
local goalPositionUp = initialPosition + Vector3.new(0, bounceHeight, 0)
local goalPositionDown = initialPosition
local upTween = tweenService:Create(bball, upTweenInfo, {Position = goalPositionUp})
local downTween = tweenService:Create(bball, downTweenInfo, {Position = goalPositionDown})
upTween.Completed:Connect(function()
downTween:Play()
end)
downTween.Completed:Connect(function()
wait(0.1)
upTween:Play()
end)
upTween:Play()
end
bball.Touched:Connect(function(hit)
if not hit then return end
local character = hit.Parent
if not character then return end
local player = game.Players:GetPlayerFromCharacter(character)
if not player then return end
print("Touched part: " .. hit.Name)
print("Character: " .. character.Name)
weldBallToHand(player)
animateBallContinuously()
end)
bball.Anchored = false
bball.CanCollide = true
bball.Velocity = Vector3.new(0, -50, 0)
print("Ball initial position: " .. tostring(bball.Position))
When I try and run it, there is no error (so clearly a logic error) but the ball slowly rises up and instead of bouncing just sort of levitates and hovers about. I expected it the bounce up and down. Do you have any idea what could be causing this?
Many thanks in advance