What did I do wrong?

I’m not sure what I did wrong, here, take a look. (Error is “Unable to Cast Value to Object”)

local gameServices = {
    tweenService = (game:GetService("TweenService"));
    playerService = (game:GetService("Players"))
}

local charVariables = {
    humRootPart = (gameServices.playerService.LocalPlayer.Character.HumanoidRootPart)
}

local tweenDefaultSettings = {
    defaultInfo = (TweenInfo.new(4, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0, false, 0))
}

local function returnCFrame(Variable)
    return (Variable.CFrame);
end

local function tweenObject(Object, Info, Properties)
    -->>  Error comes from under here.
    local temporaryTween = (gameServices.tweenService:Create(Object, Info, Properties));
    temporaryTween:Play()

    return (temporaryTween)
end

local function tweenTest()
    local temporaryCFrame = (CFrame.new(357.203949, 48.4761887, -509.941467, -0.991344094, 2.28747723e-08, -0.131291226, 1.06303597e-08, 1, 9.39623703e-08, 0.131291226, 9.1753364e-08, -0.991344094));
    tweenObject(charVariables, tweenDefaultSettings.defaultInfo, {CFrame = temporaryCFrame});

    print("Ran the script.")
end

tweenTest()

Isn’t this suppose to be tweenObject(charVariables.humRootPart,...?

1 Like

Please consider taking into consideration scripting in an adequate way.

local tweenService = (game:GetService("TweenService"));
local playerService = (game:GetService("Players"))
local LocalPlayer = playerService.LocalPlayer
local goal = CFrame.new(357.203949, 48.4761887, -509.941467, -0.991344094, 2.28747723e-08, -0.131291226, 1.06303597e-08, 1, 9.39623703e-08, 0.131291226, 9.1753364e-08, -0.991344094)
local tweenDefaultSettings = {
defaultInfo = (TweenInfo.new(4, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0, false, 0))
}
--[[
have you heard about directly getting the value from the "Variable"?
local function returnCFrame(Variable)
return (Variable.CFrame);
end]]

local function tweenObject(Object, Info, Properties)
local temporaryTween = (tweenService:Create(Object, Info, Properties));
temporaryTween:Play()

return (temporaryTween)
end

local function tweenTest()
tweenObject(LocalPlayer.Character:WaitForChild'HumanoidRootPart', tweenDefaultSettings.defaultInfo, {CFrame = goal});
print("Ran the script.")
end

tweenTest()
1 Like

Not trying to be rude or anything but telling him to script in an adequate way and not using brackets on functions like

LocalPlayer.Character:WaitForChild'HumanoidRootPart'

and using brackets on stuff that doesn’t need to use brackets like

return (temporaryTween)

is kind of strange.

4 Likes

I know it’s a bit weird right now, but I’m gonna use the function a lot more than once. I’d like to refrain from using global variables for every CFrame value.

I’m so dumb, always make mistakes like these. Let me see if it works when I replace it with what it should be.

Yeah, it is kind of weird. I guess it’s just a weird habit I have.

I was refering to his use of useless tables that the garbage collector will have to clean as a direct consequence of his coding ways. “Syntax sugar” makes no difference on the script’s performance.

That’s okay! It’s that I’m not used to that kind of formatting.

There’s nothing really wrong with it. If your script works, that’s what matters.

Ah, just habits of mine. I kind of like using tables, plus I’m gonna add a lot more to it than just one variable.

The function is quite literally filler. I didn’t intend on saying that you have to globalize every CFrame value because I assume some aren’t going to be static. You’re just putting code fluff. print(returnCFrame(workspace.Part)) and print(workspace.Part.CFrame) are the same, with the latter just being more efficient.

1 Like

If you are going to add more than just one variable, consider using methods like this:

local playersService, tweenService = game:GetService("Players"), game:GetService("TweenService")

It looks cleaner and you don’t have to type unnecessary things such as nameOfTheTable.ValueInTheTable

1 Like

True, not sure why I made a function dedicated to it. I’m new to return, so I’m practicing a bit with it.

It’s not just “unnecessary”, it impacts the code’s speed. As arrogant and clichè as I may sound, it’s good practice to localize things that are used numerous times in your code.

3 Likes