Need help with TweenService and a horn script - output says "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?
    I’m trying to make a horn sound with a smooth fade-in and fade-out. The horn sound would fade in (volume increase) when the “h” key is pressed, and it would fade out when the same key is released.

A while back, I wrote a similar thread, but it’s now obsolete, mainly because the original method I used was both unsecure and somehow broke during development (the sound would audibly play on a loop, non-stop, despite TweenService). I believe my new method works with FilteringEnabled and it also uses RemoteEvents. Maybe it’s better and less prone to the aforementioned bug?

  1. What is the issue?

image

The output yields an error that says “Unable to cast value to Object”, and the horn sound never plays because of it. I’m not sure what’s going on with the code, so I’ll paste the script and Explorer layout below.

ReplicatedStorage = game:GetService("ReplicatedStorage")
TweenService = game:GetService("TweenService")
local hornEvent = ReplicatedStorage.HornEvent
local horn = script.Parent:WaitForChild("Horn")
local sound = horn.Horn
local soundLength = sound.TimeLength
local currentTween
sound.Volume = 0
sound.Looped = true
sound:Play()

local function onHornFired(player, sound)
	if sound == true then
		--horn.Horn:Play()
		if currentTween then
			currentTween:Pause()
		end
		currentTween = TweenService:Create(sound, TweenInfo.new(soundLength), {Volume = 1})
		currentTween:Play()
	end	
	
	if sound == false then
		--horn.Horn:Stop()
		if currentTween then
			currentTween:Pause()
		end
		currentTween = TweenService:Create(sound, TweenInfo.new(soundLength), {Volume = 0})
		currentTween:Play()
	end	
end

hornEvent.OnServerEvent:Connect(onHornFired)

image

The highlighted script in the Explorer is the one above.

2 Likes

Looks to me like name conflictions. You have a variable for a sound instance called sound, but in your OnServerEvent, your 2nd parameter is also called sound, maybe change the name of one of them?

2 Likes

Dang, I never would’ve caught that! I’ve been doing a lot of work on my game, and maybe I’m honestly working overtime. I’ve been making a lot of easy-to-spot errors lately. :confused:

2 Likes

Maybe it means you have to take breaks! You shouldn’t overwork yourself! You yourself is more important than the game, give yourself time to recover from loads of work!

If you have anymore issues don’t be afraid to make another post!

3 Likes