To create a function that smoothly rotates the skateboard 360 degrees over 0.3 seconds when the player jumps, we'll need to make some adjustments to your existing scripts. Here's how you can implement this feature:
1. Modify the Server Script to Use a Motor6D
First, we need to change the way the skateboard is attached to the player's character to allow for rotation. Instead of using a WeldConstraint, we'll use a Motor6D.
In your "SkateTouchScript" (Server Script):
Replace the weldBetween function with the following code:
-- Function to create a Motor6D between a and b
local function motorBetween(a, b)
local motor = Instance.new("Motor6D")
motor.Part0 = a
motor.Part1 = b
motor.C0 = a.CFrame:inverse() * b.CFrame
motor.C1 = CFrame.new()
motor.Parent = a
return motor
end
Update the onTouch function to use motorBetween:
-- Inside the onTouch function, replace:
-- weldBetween(skate, humanoidRootPart)
-- With:
motorBetween(humanoidRootPart, skate)
2. Update the LocalScript to Handle the Skateboard Spin
Next, we'll adjust your LocalScript to detect when the player jumps and then rotate the skateboard smoothly.
In your "LocalScript" in StarterCharacterScripts:
At the top of the script, require the TweenService:
local TweenService = game:GetService("TweenService")
Add a variable to hold the reference to the Motor6D:
local motor6D -- Variable to store the Motor6D
Modify the onSkateEquipped function to find the Motor6D:
local function onSkateEquipped()
if isOnSkate then return end
isOnSkate = true
-- Disable the default Animate script
local animateScript = character:FindFirstChild("Animate")
if animateScript then
animateScript.Disabled = true
end
-- Stop all current animations
for _, track in ipairs(humanoid:GetPlayingAnimationTracks()) do
track:Stop()
end
-- Start idle animation
skateIdleTrack = humanoid:LoadAnimation(skateIdleAnimation)
skateIdleTrack.Priority = Enum.AnimationPriority.Action4
skateIdleTrack.Looped = true
skateIdleTrack:Play()
-- Prepare move animation
skateMoveTrack = humanoid:LoadAnimation(skateMoveAnimation)
skateMoveTrack.Priority = Enum.AnimationPriority.Action4
skateMoveTrack.Looped = true
-- Find the skateboard and Motor6D
local skate = character:FindFirstChild("SkateEquipped")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
if skate then
-- Find the Motor6D connecting the HumanoidRootPart to the skateboard
for _, child in ipairs(humanoidRootPart:GetChildren()) do
if child:IsA("Motor6D") and child.Part1 == skate then
motor6D = child
break
end
end
else
warn("Skateboard not found in character.")
end
-- Connect events to update animations and detect jumps
movedConnection = humanoid.Running:Connect(function(speed)
updateAnimation()
end)
stateConnection = humanoid.StateChanged:Connect(function(oldState, newState)
updateAnimation()
-- If the new state is Jumping, trigger the skateboard spin
if newState == Enum.HumanoidStateType.Jumping and motor6D then
spinSkateboard()
end
end)
end
Add the spinSkateboard function to handle the rotation:
local function spinSkateboard()
local rotationAmount = 360 -- degrees
local rotationTime = 0.3 -- seconds
-- Reset any existing Transform to prevent cumulative rotation
motor6D.Transform = CFrame.new()
-- Create the Tween for the Motor6D's Transform property
local tweenInfo = TweenInfo.new(rotationTime, Enum.EasingStyle.Linear)
local goal = {Transform = CFrame.Angles(0, math.rad(rotationAmount), 0)}
local tween = TweenService:Create(motor6D, tweenInfo, goal)
tween:Play()
-- Reset the Transform after the Tween completes
tween.Completed:Connect(function()
motor6D.Transform = CFrame.new()
end)
end
3. Ensure the Skateboard Spins When the Player Jumps
With these changes, when the player is on the skateboard and jumps, the spinSkateboard function will be called, creating a smooth 360-degree rotation over 0.3 seconds.
The rotation is achieved by tweening the Transform property of the Motor6D that connects the skateboard to the player's HumanoidRootPart. By adjusting the Transform, we can smoothly rotate the skateboard without affecting the player's movement.
Full Updated LocalScript for Reference
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")
local player = Players.LocalPlayer
local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
local SkateEquipEvent = ReplicatedStorage:WaitForChild("SkateEquipEvent")
local SkateUnequipEvent = ReplicatedStorage:WaitForChild("SkateUnequipEvent")
-- IDs das animações (substitua pelos seus IDs reais)
local skateIdleAnimationId = "rbxassetid://18254030554" -- Substitua pelo ID da sua animação de parado no skate
local skateMoveAnimationId = "rbxassetid://17363261325" -- Substitua pelo ID da sua animação de movimento no skate
-- Carregar as animações
local skateIdleAnimation = Instance.new("Animation")
skateIdleAnimation.AnimationId = skateIdleAnimationId
local skateMoveAnimation = Instance.new("Animation")
skateMoveAnimation.AnimationId = skateMoveAnimationId
local skateIdleTrack
local skateMoveTrack
local isOnSkate = false
local movedConnection
local stateConnection
local motor6D -- Variable to store the Motor6D
local function spinSkateboard()
local rotationAmount = 360 -- degrees
local rotationTime = 0.3 -- seconds
-- Reset any existing Transform to prevent cumulative rotation
motor6D.Transform = CFrame.new()
-- Create the Tween for the Motor6D's Transform property
local tweenInfo = TweenInfo.new(rotationTime, Enum.EasingStyle.Linear)
local goal = {Transform = CFrame.Angles(0, math.rad(rotationAmount), 0)}
local tween = TweenService:Create(motor6D, tweenInfo, goal)
tween:Play()
-- Reset the Transform after the Tween completes
tween.Completed:Connect(function()
motor6D.Transform = CFrame.new()
end)
end
local function updateAnimation()
if isOnSkate then
local state = humanoid:GetState()
if state == Enum.HumanoidStateType.Jumping or state == Enum.HumanoidStateType.Freefall then
-- Jogador está pulando ou caindo
if skateIdleTrack and skateIdleTrack.IsPlaying then
skateIdleTrack:Stop()
end
if skateMoveTrack and skateMoveTrack.IsPlaying then
skateMoveTrack:Stop()
end
else
-- Jogador não está pulando ou caindo
local speed = humanoid.MoveDirection.Magnitude
if speed > 0 then
-- Jogador está se movendo
if skateIdleTrack and skateIdleTrack.IsPlaying then
skateIdleTrack:Stop()
end
if skateMoveTrack and not skateMoveTrack.IsPlaying then
skateMoveTrack:Play()
end
else
-- Jogador está parado
if skateMoveTrack and skateMoveTrack.IsPlaying then
skateMoveTrack:Stop()
end
if skateIdleTrack and not skateIdleTrack.IsPlaying then
skateIdleTrack:Play()
end
end
end
end
end
local function onSkateEquipped()
if isOnSkate then return end
isOnSkate = true
-- Desativa o script de animações padrão
local animateScript = character:FindFirstChild("Animate")
if animateScript then
animateScript.Disabled = true
end
-- Para todas as animações atualmente em execução
for _, track in ipairs(humanoid:GetPlayingAnimationTracks()) do
track:Stop()
end
-- Inicia a animação de parado no skate
skateIdleTrack = humanoid:LoadAnimation(skateIdleAnimation)
skateIdleTrack.Priority = Enum.AnimationPriority.Action4 -- Definir prioridade para 'Action4' (mais alta)
skateIdleTrack.Looped = true
skateIdleTrack:Play()
-- Prepara a animação de movimento no skate
skateMoveTrack = humanoid:LoadAnimation(skateMoveAnimation)
skateMoveTrack.Priority = Enum.AnimationPriority.Action4 -- Definir prioridade para 'Action4' (mais alta)
skateMoveTrack.Looped = true
-- Encontrar o skateboard e o Motor6D
local skate = character:FindFirstChild("SkateEquipped")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
if skate then
-- Encontrar o Motor6D conectando o HumanoidRootPart ao skate
for _, child in ipairs(humanoidRootPart:GetChildren()) do
if child:IsA("Motor6D") and child.Part1 == skate then
motor6D = child
break
end
end
else
warn("Skateboard not found in character.")
end
-- Conecta aos eventos para atualizar as animações e detectar pulo
movedConnection = humanoid.Running:Connect(function(speed)
updateAnimation()
end)
stateConnection = humanoid.StateChanged:Connect(function(oldState, newState)
updateAnimation()
-- Se o novo estado for Jumping, inicia a rotação do skate
if newState == Enum.HumanoidStateType.Jumping and motor6D then
spinSkateboard()
end
end)
end
local function onSkateUnequipped()
if not isOnSkate then return end
isOnSkate = false
-- Desconecta os eventos
if movedConnection then
movedConnection:Disconnect()
movedConnection = nil
end
if stateConnection then
stateConnection:Disconnect()
stateConnection = nil
end
-- Para as animações do skate
if skateIdleTrack and skateIdleTrack.IsPlaying then
skateIdleTrack:Stop()
end
if skateMoveTrack and skateMoveTrack.IsPlaying then
skateMoveTrack:Stop()
end
-- Reativa o script de animações padrão
local animateScript = character:FindFirstChild("Animate")
if animateScript then
animateScript.Disabled = false
end
-- Opcional: Reinicia as animações padrão
if animateScript then
wait() -- Pequena espera para garantir que o script 'Animate' seja reativado
if humanoid then
for _, track in ipairs(humanoid:GetPlayingAnimationTracks()) do
track:Stop() -- Parar quaisquer animações remanescentes
end
end
end
end
-- Conecta aos eventos remotos
SkateEquipEvent.OnClientEvent:Connect(onSkateEquipped)
SkateUnequipEvent.OnClientEvent:Connect(onSkateUnequipped)
Notes:
-
Motor6D vs. WeldConstraint: By switching from a WeldConstraint to a Motor6D, we gain the ability to smoothly rotate the skateboard by modifying the Transform property without breaking the attachment to the player.
-
Tweening the Transform: The Transform property of a Motor6D allows for relative transformations on top of the existing connection between Part0 and Part1. Tweening this property enables smooth animations.
-
Resetting the Transform: After the rotation is complete, it's essential to reset the Transform property to avoid accumulating transformations over multiple jumps.
By incorporating these changes, your skateboard will now spin 360 degrees smoothly over 0.3 seconds whenever the player jumps while on the skateboard. This approach leverages the flexibility of Motor6D and client-side scripting to create a seamless and responsive experience.