Unable to cast value to object

Does anyone know why im getting the error “Unable to cast value to object” on the line that has “playlocalsound”?

This is for a UI in my game and its in a local script.

I am trying to play a click sound after everytime the person presses the button

This is for a UI in my game and its in a local script.

I am trying to play a click sound after everytime the person presses the button

local button = script.Parent
local shopui = script.Parent.Parent.Window
local soundservices = game:GetService('SoundService')
local click = game.SoundService.Click

button.MouseButton1Down:Connect(function(click)
	soundservices:PlayLocalSound(click)
	if shopui.Visible == true then
		shopui.Visible = false
		
	elseif shopui.Visible == false then
		shopui.Visible = true
		
	end
end)

Sorry, but you should probably format your entire code instead of just a snippet.

However, the main problem is that you’re doing soundservices:PlayLocalSound(click), but it’s actually grabbing the click from inside the parameters of the MouseButton1Down function, so you should probably rename your click variable from earlier.

1 Like

Alright thanks.

What do you mean about reformat my whole code?

I mean when posting on the DevForum. You should probably format the entire code into a code block instead of just the “elseif shopui.Visible part” so it’s more clean to read.

Oh yeah I didn’t mean to do that. Ill fix it.

1 Like

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