Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What would I do to fix my error for a music playing script?

I have a Regular Script that when part is touched a sound will play

--Vaiables
local Sensor = script.Parent.Sensor
local Door = script.Parent["Prison Side Door 1"]
local Window = script.Parent.Window
local Player = game.Players.LocalPlayer
local Light = script.Parent.Light
local Sensor = script.Parent.Sensor
local db = true

--make the sound ID
local music = Instance.new("Sound",Player)
music.SoundId = "rbxassetid://993959716"
music.Name = "AMusic"

--Function for everything

local function Everything(part)
	local hum = part.Parent:FindFirstChild("Humanoid")
	if db then
		if hum then
			print("HI there")
			music:Play()
		end
	end
end

Sensor.Touched:Connect(Everything)

when run, the sound does not play. What did I do wrong?

2 answers

✓ Accepted answer

Since this is a Script the player is not Defined as

game.Players.LocalPlayer

This can only be used in a LocalScript

One way you could define the player in your code is checking if the hit.Parent contains a humanoid and this should tell you if its a player like this

--Vaiables
local Sensor = script.Parent.Sensor
local Door = script.Parent["Prison Side Door 1"]
local Window = script.Parent.Window
local Light = script.Parent.Light
local Sensor = script.Parent.Sensor
local db = true

--Function for everything

local function Everything(part)
    local hum = part.Parent:FindFirstChild("Humanoid")
    if hum then
    local Player = part.Parent
    local Character = workspace:WaitForChild(Player.Name)
    local music = Instance.new("Sound",Character)
    music.SoundId = "rbxassetid://993959716"
    music.Name = "AMusic"
    if db then
     print("HI there")
     music:Play()
     end
    end
end

Sensor.Touched:Connect(Everything)

2 comments

0

I think you forgot about GetPlayerFromCharacter().

0

IT still works for \me

You're parenting the sound to the Player's player and not the character.

--Vaiables
local Sensor = script.Parent.Sensor
local Door = script.Parent["Prison Side Door 1"]
local Window = script.Parent.Window
local Player = game.Players.LocalPlayer.Character.Head -- This line was changed
local Light = script.Parent.Light
local Sensor = script.Parent.Sensor
local db = true

--make the sound ID
local music = Instance.new("Sound",Player)
music.SoundId = "rbxassetid://993959716"
music.Name = "AMusic"

--Function for everything

local function Everything(part)
    local hum = part.Parent:FindFirstChild("Humanoid")
    if db then
        if hum then
            print("HI there")
            music:Play()
        end
    end
end

Sensor.Touched:Connect(Everything)

1 comment

0

Do you understand that Character is not a part of Player in players?

This discussion is preserved as it was. New answers, comments, and votes are closed.