I am trying to create a script that makes bobbing, and when you start walking, it tweens it to a cframe function. But for some reason it keeps saying “Unable to cast to Dictionary” and I can’t seem to figure out why.
Anything helps, my script is local, and this is inside a tool. Heres my script:
local tool = script.Parent
-- added
local connection
--CFRAME CAMERA CRAP AUGH EJEPGIHJEGPOIHETGOIHFEIOPJFEI
local debounce = true
local equipped = false
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
tool.Equipped:Connect(function()
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
player.CameraMode = Enum.CameraMode.LockFirstPerson
local model = game:GetService("ReplicatedFirst").Viewmodel:Clone()
model.Name = "MODEL"
model.Parent = workspace
local tweenservice = game:GetService("TweenService")
connection = game:GetService("RunService").RenderStepped:Connect(function()
model:WaitForChild("Humanoid").Animator:LoadAnimation(model.Idle):Play()
model:SetPrimaryPartCFrame(workspace.CurrentCamera.CFrame * CFrame.new(0,0,0))
--problem starts here
if char:WaitForChild("Humanoid").MoveDirection.Magnitude > 0 then
local currentCF = workspace.CurrentCamera.CFrame
local t = tick()
local head = model
local x = math.cos(t * 2) * 0.2
local y = math.abs(math.sin(t * 5)) * 0.1
local cf = currentCF * CFrame.new(x, y, 0)
head:SetPrimaryPartCFrame(cf)
else
local tweeninfo = TweenInfo.new(1, Enum.EasingStyle.Exponential, Enum.EasingDirection.In, 0,false,0)
local goal = {model.CameraBone}
tweenservice:Create(model,tweeninfo,goal):Play() -- "Unable to cast to dictionary"
end
end)
-- problem ends here
local function Hidegunl(transparency)
for i,v in pairs(tool:GetChildren()) do
if (v:IsA("BasePart")) then -- Only baseparts have a LocalTransparencyModifier property.
v.LocalTransparencyModifier = transparency;
end
end
end
game:GetService("RunService").RenderStepped:Connect(function()
Hidegunl(1)
end)
end)
tool.Unequipped:Connect(function()
equipped = false
local player = game.Players.LocalPlayer
local icon = player:GetMouse().Icon
player.CameraMode = Enum.CameraMode.Classic
-- added
connection:Disconnect() -- prevent that function in RenderStepped from running again and printing errors
workspace:WaitForChild("MODEL"):Destroy()
end)
Basically the humanoidrootpart of the model, it controls the camera. But it also acts as the actual camera because humanoidrootpart is the main primary part. And with my animation, its continuously moving the whole model. so it would be tweening it to its exact position. I’m trying to figure out how to tween the entire model to the position of the currentcamera.
I just want to point out problems in this code.
First of all, these problems are inside a RenderStepped connection, which runs about ~60 times per second.
You can load the animation outside of the connection, then play it inside
local animIdle = model:WaitForChild("Humanoid").Animator:LoadAnimation(model.Idle)
connection = game:GetService("RunService").RenderStepped:Connect(function()
animIdle:Play()
.
Set a local variable for the humanoid outside of the connection. Calling WaitForChild repeatitively may lag the player.
local humanoid = char:WaitForChild("Humanoid")
...
connection = game:GetService("RunService").RenderStepped:Connect(function()
...
if humanoid.MoveDirection.Magnitude > 0 then
.
You are instantly setting the model’s position to the camera, yet your problem is tweening to the camera? Plus, multiplying a CFrame by 0,0,0 or identity changes nothing.
Let’s say you are not moving (MoveDirection.Magnitude == 0).
Like the previous problem, you already set the model to the camera cframe, yet you want to tween the model to the camera cframe.
tool.Equipped:Connect(function()
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
player.CameraMode = Enum.CameraMode.LockFirstPerson
local model = game:GetService("ReplicatedFirst").Viewmodel:Clone()
model.Name = "MODEL"
model.Parent = workspace
local humanoid = char:WaitForChild("Humanoid")
local tweenservice = game:GetService("TweenService")
local animIdle = model:WaitForChild("Humanoid").Animator:LoadAnimation(model.Idle)
local wasMoving = false
connection = game:GetService("RunService").RenderStepped:Connect(function()
animIdle:Play()
if humanoid.MoveDirection.Magnitude > 0 then
local currentCF = workspace.CurrentCamera.CFrame
local t = time()
local head = model
local x = math.cos(t * 2) * 0.2
local y = math.abs(math.sin(t * 5)) * 0.1
local cf = currentCF * CFrame.new(x, y, 0)
head:SetPrimaryPartCFrame(cf)
wasMoving = true
elseif wasMoving then
local tweeninfo = TweenInfo.new(1, Enum.EasingStyle.Exponential, Enum.EasingDirection.In, 0,false,0)
local goal = {CFrame = workspace.CurrentCamera.CFrame}
tweenservice:Create(model.CameraBone,tweeninfo,goal):Play()
wasMoving = false
end
end)
local function Hidegunl(transparency)
for i,v in pairs(tool:GetChildren()) do
if (v:IsA("BasePart")) then -- Only baseparts have a LocalTransparencyModifier property.
v.LocalTransparencyModifier = transparency;
end
end
end
game:GetService("RunService").RenderStepped:Connect(function()
Hidegunl(1)
end)
end)
Ah, I see, you will need Lerping instead of Tweening.
tool.Equipped:Connect(function()
local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
player.CameraMode = Enum.CameraMode.LockFirstPerson
local model = game:GetService("ReplicatedFirst").Viewmodel:Clone()
model.Name = "MODEL"
model.Parent = workspace
local humanoid = char:WaitForChild("Humanoid")
local animIdle = model:WaitForChild("Humanoid").Animator:LoadAnimation(model.Idle)
local lerpSpeed = 10 -- You can modify this
connection = game:GetService("RunService").RenderStepped:Connect(function(dt)
animIdle:Play()
if humanoid.MoveDirection.Magnitude > 0 then
local currentCF = workspace.CurrentCamera.CFrame
local t = time()
local head = model
local x = math.cos(t * 2) * 0.2
local y = math.abs(math.sin(t * 5)) * 0.1
local cf = currentCF * CFrame.new(x, y, 0)
head:SetPrimaryPartCFrame(cf)
else
model.CameraBone.CFrame = model.CameraBone.CFrame:Lerp(workspace.CurrentCamera.CFrame,dt*lerpSpeed)
end
end)
local function Hidegunl(transparency)
for i,v in pairs(tool:GetChildren()) do
if (v:IsA("BasePart")) then -- Only baseparts have a LocalTransparencyModifier property.
v.LocalTransparencyModifier = transparency;
end
end
end
game:GetService("RunService").RenderStepped:Connect(function()
Hidegunl(1)
end)
end)