Unable to cast value to object

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a sound fade out
  2. What is the issue? Include screenshots / videos if possible!
    The script gives me an error ‘Unable to cast value to object’
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I couldn’t find any solutions
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
local FacilityMusic = script:WaitForChild("FacilityMusic")
local HouseMusic = script:WaitForChild("HouseMusic")
local ShrineMusic = script:WaitForChild("ShrineMusic")
local Static = script:WaitForChild("StaticSound")
function Facility()
	TS:Create(FacilityMusic, TweenInfo.new(2), {Volume = 1}):Play()
	TS:Create(HouseMusic, TweenInfo.new(2), {Volume = 0}):Play()
	TS:Create(ShrineMusic, TweenInfo.new(2), {Volume = 0}):Play()
	TS:Create(Static, TweenInfo.new(2), {Volume = 0}):Play() -- the line that the error happens on
	while CurrentMusic == "Facility" do wait() end
	TS:Create(FacilityMusic, TweenInfo.new(2), {Volume = 0}):Play()
	spawn(Music)
end

image

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

2 Likes

You could try something like this,

local Tween = game:GetService("TweenService")
local Sounds = {path}

local function Facility()
	for _, Sound in pairs(Sounds:GetChildren()) do
		if Sound.Name ~= "FacilityMusic" then
			Tween:Create(Sound, TweenInfo.new(2), {Volume = 0}):Play()
		end
	end
end

I feel like it’s more simple to just do them one at a time since there are only 3 other sounds

What is this supposed to be?

Music() is a function that chooses the next music

It would appear to me that ‘Static’ is a value, instead of an object. The error is saying you the function needs an object type, but you’re passing in a value type, which it can not convert to object. So either you’re setting it somewhere else in the script, or it’s another problem

1 Like

On what line is the error? And can you tell us what you are trying to do? Giving us your broken script with 0 context and explanation isn’t helping.

Here’s the rest of the script

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local TS = game:GetService("TweenService")

local MovingPart = game.Workspace:WaitForChild("MovingPart")
local StartPart = game.Workspace:WaitForChild("StartPart")
local GoalPart = game.Workspace:WaitForChild("GoalPart")
local SecretPart = game.Workspace:WaitForChild("DoorClosePos")
local SecretTruss = game.Workspace:WaitForChild("SecretTruss")
local SecretPlatform = game.Workspace:WaitForChild("SecretPlatform")
local WinPart = game.Workspace:WaitForChild("WinPart")
local CC = game.Lighting:WaitForChild("ColorCorrection")
local FadeOut = TS:Create(CC, TweenInfo.new(2), {TintColor = Color3.fromRGB(0, 0, 0)})

local FacilityStart = game.Workspace:WaitForChild("_FacilityStart")
local FacilityStop = game.Workspace:WaitForChild("_FacilityStop")
local StaticEnd = game.Workspace:WaitForChild("_StaticEnd")
local StaticStart = game.Workspace:WaitForChild("_StaticStart")
local StaticStop = game.Workspace:WaitForChild("_StaticStop")
local HouseBarrier = game.Workspace:WaitForChild("_HouseBarrier")
local HouseStart = game.Workspace:WaitForChild("_HouseStart")
local NonCanCollideModel = game.Workspace:WaitForChild("NonCanCollideModel")
local NoTurningBack = game.Workspace:WaitForChild("NoTurningBack")
local Door = NoTurningBack:WaitForChild("Door")
local Barrier = NoTurningBack:WaitForChild("Barrier")
local SecretButton = game.Workspace:WaitForChild("_SecretButton")

local FacilityMusic = script:WaitForChild("FacilityMusic")
local DarkRoom = script:WaitForChild("DarkRoom")
local HouseMusic = script:WaitForChild("HouseMusic")
local ShrineMusic = script:WaitForChild("ShrineMusic")
local Static = script:WaitForChild("StaticSound")
local Jumpscare = script:WaitForChild("Jumpscare")

local CurrentMusic = "Facility"
local debounce = false

MovingPart.Position = StartPart.Position

WinPart.Touched:Connect(function()
	FadeOut:Play()
end)

FadeOut.Completed:Connect(function()
	task.wait(1)
	player:Kick("Please wait for all other players to finish the trial.")
end)

function Facility()
	TS:Create(FacilityMusic, TweenInfo.new(2), {Volume = 1}):Play()
	TS:Create(HouseMusic, TweenInfo.new(2), {Volume = 0}):Play()
	TS:Create(ShrineMusic, TweenInfo.new(2), {Volume = 0}):Play()
	TS:Create(Static, TweenInfo.new(2), {Volume = 0}):Play() -- error is here, i'm trying to make the volume go down with tweens
	while CurrentMusic == "Facility" do wait() end
	TS:Create(FacilityMusic, TweenInfo.new(2), {Volume = 0}):Play()
	spawn(Music)
end

function House()
	TS:Create(HouseMusic, TweenInfo.new(2), {Volume = 0.2}):Play()
	TS:Create(FacilityMusic, TweenInfo.new(2), {Volume = 0}):Play()
	TS:Create(ShrineMusic, TweenInfo.new(2), {Volume = 0}):Play()
	TS:Create(Static, TweenInfo.new(2), {Volume = 0}):Play()
	while CurrentMusic == "House" do wait() end
	TS:Create(HouseMusic, TweenInfo.new(2), {Volume = 0}):Play()
	spawn(Music)
end

function Shrine()
	TS:Create(ShrineMusic, TweenInfo.new(0.5), {Volume = 0.1}):Play()
	TS:Create(FacilityMusic, TweenInfo.new(0.5), {Volume = 0}):Play()
	TS:Create(HouseMusic, TweenInfo.new(0.5), {Volume = 0}):Play()
	TS:Create(Static, TweenInfo.new(0.5), {Volume = 0}):Play()
end

function Static()
	local StaticTween = TS:Create(Static, TweenInfo.new(20), {Volume = 1})
	StaticTween:Play()
	TS:Create(FacilityMusic, TweenInfo.new(2), {Volume = 0}):Play()
	TS:Create(HouseMusic, TweenInfo.new(2), {Volume = 0}):Play()
	TS:Create(ShrineMusic, TweenInfo.new(2), {Volume = 0}):Play()
	while CurrentMusic == "Static" do wait() end
	StaticTween:Cancel()
	TS:Create(Static, TweenInfo.new(2), {Volume = 0}):Play()
	spawn(Music)
end

function None()
	TS:Create(FacilityMusic, TweenInfo.new(2), {Volume = 0}):Play()
	TS:Create(HouseMusic, TweenInfo.new(2), {Volume = 0}):Play()
	TS:Create(ShrineMusic, TweenInfo.new(2), {Volume = 0}):Play()
	TS:Create(Static, TweenInfo.new(2), {Volume = 0}):Play()
	while CurrentMusic == "None" do wait() end
	spawn(Music)
end

function Music()
	if CurrentMusic == "Facility" then
		spawn(Facility)
	end
	if CurrentMusic == "House" then
		spawn(House)
	end
	if CurrentMusic == "Shrine" then
		spawn(Shrine)
	end
	if CurrentMusic == "Static" then
		spawn(Static)
	end
	if CurrentMusic == "None" then
		spawn(None)
	end
end

FacilityStart.Touched:Connect(function()
	CurrentMusic = "Facility"
end)

FacilityStop.Touched:Connect(function()
	CurrentMusic = "None"
end)

StaticStop.Touched:Connect(function()
	CurrentMusic = "None"
end)

StaticStart.Touched:Connect(function()
	CurrentMusic = "Static"
end)

StaticEnd.Touched:Connect(function()
	CurrentMusic = "None"
end)

HouseStart.Touched:Connect(function()
	CurrentMusic = "House"
	HouseBarrier.CanCollide = true
end)

NoTurningBack.Touched:Connect(function()
	if debounce then
		return
	end
	debounce = true
	DarkRoom.Volume = 0.5
	DarkRoom:Play()
	CurrentMusic = "Shrine"

	Door.CanCollide = true
	Barrier.CanCollide = true
	TS:Create(game.Lighting, TweenInfo.new(0.5), {Ambient = Color3.fromRGB(0, 0, 0)}):Play()
end)

spawn(Music)

if script.Parent.Name == "Light1000_BOTTLE" then
	MovingPart.CanCollide = true
	MovingPart.Transparency = 0
	SecretTruss.CanCollide = true
	SecretTruss.Transparency = 0
	SecretPlatform.CanCollide = true
	SecretPlatform.Transparency = 0
	SecretPart.CanCollide = false
	for i, v in pairs(NonCanCollideModel:GetChildren()) do
		if v:IsA("BasePart") then
			v.Transparency = 1
			v.CanCollide = false
		end
	end
	task.wait(4)
	TS:Create(MovingPart, TweenInfo.new(7, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, -1, true, 0.5), {Position = GoalPart.Position}):Play()
end

while SecretButton.Color == Color3.fromRGB(0, 255, 0) do wait() end

task.wait(10)

if debounce then
	Jumpscare.Volume = 2
	Jumpscare:Play()
	TS:Create(Jumpscare, TweenInfo.new(3), {Volume = 0}):Play()
end

It’s an object and I only set the variable at the top of the script

Well, I can see that you create a function called Static. That means you’re overwriting the variable.

2 Likes

You named a function Static too, so it thinks you’re referencing the function. Just rename your sound variable to StaticSound and change the variable names.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.