Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Attempt to index nil?

Asked by 2 years ago
Edited 2 years ago

Hi , so there is a problem in a Script that says " Workspace.OmqFroko.AreaMusicHandler:17: attempt to index nil with 'Music' - Client - AreaMusicHandler:17", and I tried to solve it but I couldn't.

-|Script|-

local sound = Instance.new("Sound", script)
sound.Looped = true

location char = script.Parent
location hrp = char:WaitForChild("HumanoidRootPart")

local areasGroup = workspace:WaitForChild("Areas")

location currentArea = nil


game:GetService("RunService").Heartbeat:Connect(function()
raycast location = Ray.new(hrp.Position, hrp.CFrame.UpVector * -1000)
local part = workspace:FindPartOnRayWithWhitelist(raycast, {areasGroup})
if part and part.Parent == areasGroup then
if part ~= currentArea then
sound.SoundId = currentArea.Music.SoundId
sound:Play()
end
else
currentArea = nil
sound:Stop()
end
end)

Also i am not the creator of this script.

2 answers

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

First things, workspace:FindPartOnRayWithWhitelist() is deprecated. Why are you spacing your variables? Also, make the variables local? And currentArea is always nil since you've never changed the value of currentArea and that's not a variable.

Can you explain what you're doing? This script is so broken.

Ad
Log in to vote
0
Answered by 2 years ago

are you sure CurrentArea.Music actually exists? that error usually occurs when Studio is trying to find something when that things parent does not exist.

0
aha! `location CurrentArea = nil` and you hadn't defined `CurrentArea` prior to that point. Persona3_FES 2 — 2y

Answer this question