Hello everyone, now I am writing a local script for the tool and the animation only works on the client side. Вesides this, there is also a trail that also works only on the client side.
I need the animation and trail to work on the server side and on the client side.
I tried to find a solution to this problem on the DevForum, where it was advised to use remotes, but I do not quite understand how to use them in my case.
Client side:
Server side:
Here’s what’s inside the tool:
Here is the local script:
local runService = game:GetService("RunService")
local contextActionService = game:GetService("ContextActionService")
local connection = nil
local gravityVector = Vector3.new(0, game.Workspace.Gravity, 0)
local yAxis = 0
local ForceValue = script:WaitForChild("ForceValue")
local force = ForceValue.Value
local drag = 1
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character
if not character or not character.Parent then
character = player.CharacterAdded:wait()
end
local primaryPart = character.PrimaryPart
local tool = script.Parent
local vectorForce = script:WaitForChild("VectorForce")
vectorForce.Attachment0 = primaryPart.RootRigAttachment
local alignOrientation = script:WaitForChild("AlignOrientation")
alignOrientation.Attachment0 = primaryPart.RootRigAttachment
local animation = script:WaitForChild("FlyAnimation")
local animationTrack = character.Humanoid.Animator:LoadAnimation(animation)
animationTrack.Priority = Enum.AnimationPriority.Movement
local attachment0 = Instance.new("Attachment")
attachment0.Position = Vector3.new(-2, 0, 0)
attachment0.Parent = character:WaitForChild("LowerTorso")
local attachment1 = Instance.new("Attachment")
attachment1.Position = Vector3.new(2, 0, 0)
attachment1.Parent = character:WaitForChild("LowerTorso")
local trail = script:WaitForChild("Trail")
trail.Attachment0 = attachment0
trail.Attachment1 = attachment1
local function FlyAction(actionName, inputState, inputObject)
if inputState ~= Enum.UserInputState.Begin then
return Enum.ContextActionResult.Pass
end
if connection == true then return Enum.ContextActionResult.Pass end
if connection == nil then
connection = true
if character.Humanoid.FloorMaterial ~= Enum.Material.Air then
character.Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
task.wait(0.1)
end
vectorForce.Enabled = true
alignOrientation.CFrame = primaryPart.CFrame
alignOrientation.Enabled = true
animationTrack:Play()
animationTrack.Looped = true
trail.Enabled = true
character.Humanoid:ChangeState(Enum.HumanoidStateType.Physics)
connection = runService.Heartbeat:Connect(function(deltaTime)
vectorForce.Force = gravityVector * primaryPart.AssemblyMass
local moveVector = Vector3.new(character.Humanoid.MoveDirection.X, yAxis, character.Humanoid.MoveDirection.Z)
if moveVector.Magnitude > 0 then
moveVector = moveVector.Unit
vectorForce.Force += moveVector * force * primaryPart.AssemblyMass
if math.abs(moveVector.Y) == 1 then
alignOrientation.CFrame = CFrame.lookAt(Vector3.new(0,0,0), moveVector, -primaryPart.CFrame.LookVector) * CFrame.fromOrientation(-math.pi/ 6, 0, 0)
else
alignOrientation.CFrame = CFrame.lookAt(Vector3.new(0,0,0), moveVector) * CFrame.fromOrientation(-math.pi/ 6, 0, 0)
end
alignOrientation.CFrame = CFrame.lookAt(Vector3.new(0,0,0), moveVector) * CFrame.fromOrientation(-math.pi/ 6, 0, 0)
end
if primaryPart.AssemblyLinearVelocity.Magnitude > 0 then
local dragVector = -primaryPart.AssemblyLinearVelocity.Unit
local v2 = primaryPart.AssemblyLinearVelocity.Magnitude ^ 1.2
vectorForce.Force += dragVector * drag * primaryPart.AssemblyMass * v2
end
end)
else
vectorForce.Enabled = false
alignOrientation.Enabled = false
animationTrack:Stop()
animationTrack.Looped = false
trail.Enabled = false
character.Humanoid:ChangeState(Enum.HumanoidStateType.Freefall)
connection:Disconnect()
connection = nil
end
return Enum.ContextActionResult.Pass
end
local function UpAction(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then yAxis = 1 else yAxis = 0 end
return Enum.ContextActionResult.Pass
end
local function DownAction(actionName, inputState, inputObject)
if inputState == Enum.UserInputState.Begin then yAxis = -1 else yAxis = 0 end
return Enum.ContextActionResult.Pass
end
tool.Equipped:Connect(function()
contextActionService:BindAction("Fly", FlyAction, true, Enum.KeyCode.F)
contextActionService:SetTitle("Fly", "Fly")
contextActionService:SetPosition("Fly", UDim2.new(1, -150, 1, -80))
contextActionService:BindAction("Up", UpAction, true, Enum.KeyCode.E)
contextActionService:SetTitle("Up", "Up")
contextActionService:SetPosition("Up", UDim2.new(1, -55, 1, -145))
contextActionService:BindAction("Down", DownAction, true, Enum.KeyCode.Q)
contextActionService:SetTitle("Down", "Down")
contextActionService:SetPosition("Down", UDim2.new(1, -105, 1, -145))
end)
tool.Unequipped:Connect(function()
contextActionService:UnbindAction("Fly")
contextActionService:UnbindAction("Up")
contextActionService:UnbindAction("Down")
character.Humanoid:ChangeState(Enum.HumanoidStateType.Freefall)
script.VectorForce.Enabled = false
script.AlignOrientation.Enabled = false
script.Trail.Enabled = false
animationTrack:Stop()
animationTrack.Looped = false
end)