I made a very bad vaulting script,but when i vault the serverside character position and the clientside character position are completely different,and that breaks a lot of stuff.
I tried searching on forum for that,but couldnt understand anything.
Heres a video of the problem:
(if the video doesnt work then click this link)
And the serverscript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")
local DebouncePlrs = {}
local StaminaRecoveryDebounce = {}
local StaminaRecoveryValue = 200
-- I am afraid of this thing
local function VisualizeRay(origin,hitposition,brickcolor)
--[[
local position = hitposition
local distance = (origin - position).Magnitude
local p = Instance.new("Part")
p.Anchored = true
p.CanCollide = false
p.Size = Vector3.new(0.1, 0.1, distance)
p.CFrame = CFrame.lookAt(position, origin)*CFrame.new(0, 0, -distance/2)
p.Parent = workspace
p.Material = Enum.Material.Neon
p.BrickColor = brickcolor
p.CanQuery = false
]]
end
ReplicatedStorage.Events.Movement.Jump.OnServerEvent:Connect(function(plr)
local char = plr.Character
local Humanoid = plr.Character.Humanoid
local HRP = char.HumanoidRootPart
local MaxVaultDistanceFromPart = 13 + 1
local function Jump() -- if the vaulting requirements are not met then make the player jump with this function
Humanoid.JumpPower = 20
Humanoid.Jump = true
task.wait(0.5)
Humanoid.JumpPower = 0
end
local RayParams = RaycastParams.new()
RayParams.FilterType = Enum.RaycastFilterType.Exclude
RayParams.FilterDescendantsInstances = {char,char["Left Arm"],char["Left Leg"],char["Right Leg"],char["Right Arm"],char["Head"],char["Camera"] }
if DebouncePlrs[plr.Name] == nil then
DebouncePlrs[plr.Name] = true
if Humanoid.MoveDirection.Magnitude > 0 and Humanoid.WalkSpeed > 0 then -- if moving
local function Vault(target,ResultLow)
if char["IsRunning?"].Value == true and char.Stamina.Value < (1000-StaminaRecoveryValue) and StaminaRecoveryDebounce[plr.Name] == nil then char.Stamina.Value += StaminaRecoveryValue; StaminaRecoveryDebounce[plr.Name] = true end
print("Vaulting!!!!!!")
local TargetPosition = (target.CFrame * CFrame.new(0, target.Size.Y / 2, 0) * CFrame.Angles(math.pi / 2, 0, 0) + Vector3.new(0,5,0))
HRP.Anchored = true
local Tween = TweenService:Create(HRP,TweenInfo.new(Humanoid.WalkSpeed / (1 + (460 * Humanoid.WalkSpeed / 26 ))),{["Position"] = ResultLow.Position + Vector3.new(0,target.Size.Y,0)})
Tween:Play()
Tween.Completed:Wait()
local i = 0
repeat
local Tween2 = TweenService:Create(HRP,TweenInfo.new(Humanoid.WalkSpeed / (1 + (920 * Humanoid.WalkSpeed / 26))),{["Position"] = HRP.Position + HRP.CFrame.LookVector})
local RaycastVaultDashThing = workspace:Raycast(HRP.Position,HRP.CFrame.LookVector*100,RayParams)
if RaycastVaultDashThing then
if (RaycastVaultDashThing.Position - HRP.Position).Magnitude > 5 then
Tween2:Play()
Tween2.Completed:Wait()
end
else
Tween2:Play()
Tween2.Completed:Wait()
end
i += 1
until i == 5
HRP.Anchored = false
local crtin = coroutine.create(function()
task.wait(2)
StaminaRecoveryDebounce[plr.Name] = nil
end)
coroutine.resume(crtin)
end
local ResultLowOffsetOrigin = 0.15
local ResultLowOffsetDirection = 0.15
local Result1 = workspace:Raycast(HRP.Position,HRP.CFrame.LookVector*100,RayParams) -- check if theres something in front the torso level of the character
local ResultLow = workspace:Raycast(Vector3.new(HRP.Position.X,HRP.Position.Y - ResultLowOffsetOrigin,HRP.Position.Z),Vector3.new(HRP.CFrame.LookVector.X,HRP.CFrame.LookVector.Y - ResultLowOffsetDirection,HRP.CFrame.LookVector.Z)*100,RayParams) -- check if theres something infront of the leg level of the character
-- Result1 is from HRP
-- ResultLow is from around the leg level
if ResultLow and ResultLow.Instance.Name ~= "Baseplate" then
if Result1 then
VisualizeRay(HRP.Position,Result1.Position,BrickColor.new("Yellow flip/flop"))
VisualizeRay(Vector3.new(HRP.Position.X,HRP.Position.Y - ResultLowOffsetOrigin,HRP.Position.Z),ResultLow.Position,BrickColor.new("Baby blue"))
print("Result1: " .. Result1.Instance.Name)
print("ResultLow: " .. ResultLow.Instance.Name)
if Result1.Instance ~= ResultLow.Instance and (HRP.Position - ResultLow.Instance.Position).Magnitude < MaxVaultDistanceFromPart then -- Checking if the part is not higher than character's legs
Vault(ResultLow.Instance,ResultLow)
else
Jump()
end
elseif (HRP.Position - ResultLow.Instance.Position).Magnitude < MaxVaultDistanceFromPart then
print("ONLY ResultLow: " .. ResultLow.Instance.Name)
VisualizeRay(Vector3.new(HRP.Position.X,HRP.Position.Y - ResultLowOffsetOrigin,HRP.Position.Z),ResultLow.Position,BrickColor.new("Baby blue"))
Vault(ResultLow.Instance,ResultLow)
end
else
Jump()
end
else -- if not moving
Jump()
end
DebouncePlrs[plr.Name] = nil
end
end)
How can i make this script keep the serverside position and the client position the same?
Thanks in advance.