Unable to cast value to Object

Hey i’ve been trying to make my character get a trail, but for some reason the code I’m using is not working. I’m pretty new scripting so this may be a easy fix question. Here is the script below, its in StarterChracterScript. I keep getting this errorimage




local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()



local Attachment0 = Instance.new("Attachment")
Attachment0.Parent = player.Character.Humanoid:GetBodyPartR15('UpperTorso')

local Attachment1 = Instance.new("Attachment") 
Attachment0.Parent = player.Character.Humanoid:GetBodyPartR15('LowerTorso')

local Trail = replicatedStorage.Trail
local newTrail = Trail:Clone()
Trail.Attachment0 = Attachment0
Trail.Attachment1 = Attachment1

newTrail.Parent = player
print('test')

Which line is the error occurring on?

Attachment0.Parent = player.Character.Humanoid:GetBodyPartR15(‘UpperTorso’)

and

Attachment0.Parent = player.Character.Humanoid:GetBodyPartR15(‘LowerTorso’)

GetBodyPartR15 returns an enum (BodyPartR15), not a part. You can not set the parent of any instance to an enum value.

Furthermore, the parameter passed is supposed to be the part itself, not the name of it. You are essentially using this method backwards.

I would recommend :WaitForChild() instead.

Try this code sample:

local replicatedStorage = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local Attachment0 = Instance.new("Attachment")
Attachment0.Parent = player.Character:WaitForChild("UpperTorso")

local Attachment1 = Instance.new("Attachment") 
Attachment1.Parent = player.Character:WaitForChild("LowerTorso")

local Trail = replicatedStorage.Trail
local newTrail = Trail:Clone()
Trail.Attachment0 = Attachment0
Trail.Attachment1 = Attachment1

newTrail.Parent = character

Note: Using a local script in order to create these trails prevents other players from viewing them. If you want replicated trails, make sure you use a server script.

1 Like

This returns an enum. Read this:

Why not try player.Character:FindFirstChild(“UpperTorso”)
or what @Galactiq said.

Thanks that makes sense, I’ll try it

Thanks alot @Galactiq and @Cats767_99 it works