Player model is teleporting back to original position after destroying weld for my rotation script
SERVER SIDED SCRIPT
local event = game.ReplicatedStorage:WaitForChild("Rotationevent")
local StopEvent = game.ReplicatedStorage:WaitForChild("StopRotEvent")
event.OnServerEvent:Connect(function(plr, randomX, randomY)
local ISROTATING = true
StopEvent.OnServerEvent:Connect(function(plr)
ISROTATING = false
end)
local function ROTATE(plr)
local char = plr.Character or plr.CharacterAdded:Wait()
local CANFLY = char:GetAttribute("Canfly")
local sun = workspace:WaitForChild("SUN")
local distance = (char.PrimaryPart.Position - sun.Position).Magnitude
local duration = distance / 100
local part = Instance.new("Part")
local weld = Instance.new("WeldConstraint")
part.Position = sun.Position
part.Parent = workspace
part.CFrame = CFrame.Angles(char.PrimaryPart.CFrame.Rotation.X,char.PrimaryPart.CFrame.Rotation.Y,-char.PrimaryPart.CFrame.Rotation.Z)
weld.Part0 = part
weld.Part1 = char.PrimaryPart
weld.Parent = part
local oldcframe
CANFLY = false
while wait(distance * 0.0005) do
print(ISROTATING)
if ISROTATING == true then
part.CFrame = part.CFrame * CFrame.Angles(math.rad(randomX),math.rad(randomY),math.rad(0))
else
part:Destroy()
weld:Destroy()
CANFLY = true
ISROTATING = false
break
end
end
end
ROTATE(plr)
end)
CLIENT SIDED SCRIPT
local sun = game.Workspace:WaitForChild("SUN")
local button = script.Parent
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local RANDOM_VALUES = {
0.1, 0.09, 0.08, 0.07, 0.06, 0.05, -0.1, -0.09, -0.08, -0.07, -0.06, -0.05,
}
local CANFLY = char:GetAttribute("Canfly")
local ISROTATING = false
local event = game.ReplicatedStorage:WaitForChild("Rotationevent")
local StopEvent = game.ReplicatedStorage:WaitForChild("StopRotEvent")
button.MouseButton1Down:Connect(function()
if not ISROTATING then
local distance = (char.PrimaryPart.Position - sun.Position).Magnitude
local duration = distance / 100
local part = Instance.new("Part")
local weld = Instance.new("WeldConstraint")
part.Position = sun.Position
part.Anchored = true
part.Parent = workspace
part.CFrame = CFrame.Angles(char.PrimaryPart.CFrame.Rotation.X,char.PrimaryPart.CFrame.Rotation.Y,-char.PrimaryPart.CFrame.Rotation.Z)
weld.Part0 = part
weld.Part1 = char.PrimaryPart
weld.Parent = part
local oldcframe = char.PrimaryPart.CFrame
CANFLY = false
ISROTATING = true
local randomX = math.random(1, 12)
local randomY = math.random(1, 12)
event:FireServer(RANDOM_VALUES[randomX], RANDOM_VALUES[randomY])
while true do
if ISROTATING == true then
wait(distance * 0.0005)
part.CFrame = part.CFrame * CFrame.Angles(math.rad(RANDOM_VALUES[randomX]),math.rad(RANDOM_VALUES[randomY]),math.rad(0))
else
weld:Destroy()
part:Destroy()
break
end
end
else
StopEvent:FireServer()
ISROTATING = false
CANFLY = true
end
end)
Anyone know a fix?