I am trying to create a piggyback system. What I have so far is a command that lets you piggyback someone. I have tried welding the characters but it was such a big mess and the players kept falling in the ground. I have moved to renderstepped to move the character to the target players CFrame, but its shakey. Here is the local script and server script.
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Players = game.Players
local RunService = game:GetService("RunService")
local Riding = false
local Anim = Character:WaitForChild("Humanoid"):LoadAnimation(script.Animation)
Player.Chatted:Connect(function(Message)
local splitMessage = Message:split(" ")
print("SPOKEN")
if splitMessage[1]:lower() == "!piggyback" then
print("1")
for i, v in pairs(game.Players:GetChildren()) do
print("GETTING PLAYERS")
print(splitMessage[2])
if v.Name == splitMessage[2] then
print("2")
Riding = false
wait(1)
Riding = true
RunService.RenderStepped:Connect(function()
if Riding == true then
game.ReplicatedStorage.Events.PiggyBack:FireServer(v.Character, true)
Anim:Play()
end
end)
end
end
end
end)
-- Local script
game.ReplicatedStorage.Events.PiggyBack.OnServerEvent:Connect(function(Player, TargetPlayer, Value)
if Value == true then
Player.Character.LowerTorso.CFrame = TargetPlayer.Head.CFrame
Player.Character.HumanoidRootPart.Anchored = true
end
end)
-- Server script
I really just want this to work. Is there anyway someone here could help?
So the first things I see is that you’re sending an unholy amount of FireServers, which isn’t optimal.
What I would do is convert this to a regular script, use the PlayerAdded event and then use the parameter it gives you to use the Chatted event, and you would have to use HeartBeat rather than RenderStepped, which should’ve been done anyways as that is ran after physics calculations. And I would just put the movement code in Heartbeat event and once Riding is false or you reset/ your character is gone, I’d disconnect the event. Maybe try something like this in a Server script instead?
local Character = Player.Character or Player.CharacterAdded:Wait()
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local Riding = false
local Anim = Character:WaitForChild("Humanoid"):LoadAnimation(script.Animation)
local connection
Players.PlayerAdded:Connect(function(Player)
Player.Chatted:Connect(function(Message)
local splitMessage = Message:split(" ")
print("SPOKEN")
if splitMessage[1]:lower() == "!piggyback" then
print("1")
for i, v in pairs(Players:GetPlayers()) do
print("GETTING PLAYERS")
print(splitMessage[2])
if v.Name == splitMessage[2] then
print("2")
Riding = false
wait(1)
Riding = true
connection = RunService.Heartbeat:Connect(function()
if Riding then
Player.Character.LowerTorso.CFrame = v.Character.Head.CFrame
Player.Character.HumanoidRootPart.Anchored = true
Anim:Play()
elseif not Riding or not Player.Character then --If not riding or your character doesn't exist anymore, ex, you reset, disconnect
connection:Disconnect()
end
end)
end
end
end
end)
end)
Also you really opt to use guard clauses since there’s a lot of if statements and indentation. Please tell me if something doesn’t work!