Jump to content

ImpactNoise.lua broken


Azure_Zero
 Share

Recommended Posts

I've been following the Project Saturn Tutorials and got to the point with the barrels

but I get the Error:

Attempt to get Length of Field 'sound' (a nil value)

 

How do I fix it.

 

--[[-------------------------------------------------------------------
This is a simple script to provide an impact sound for a physically
interactive object.
]]---------------------------------------------------------------------
Script.soundfile1=""--path "Sound 1" "Wac file (*.wav):wav|Sound"
Script.soundfile2=""--path "Sound 2" "Wac file (*.wav):wav|Sound"
Script.soundfile3=""--path "Sound 3" "Wac file (*.wav):wav|Sound"
Script.soundfile4=""--path "Sound 4" "Wac file (*.wav):wav|Sound"
Script.threshhold=2--float "Threshhold"
Script.maxfrequency=300--minimum delay between sound plays
Script.range=20--float "Range"
--Global values
ImpactNoiseLastSoundTime=0-- This will be shared among all instances of this script and ensure we don't play too many sounds
function Script:Start()
self.sound={}
for n=1,4 do
 local filepath = self["soundfile"..tostring(n)]
 if filepath~="" then
  local noise = Sound:Load(filepath)
  if noise~=nil then
   table.insert(self.sound,noise)
   --self.sound[#self.sound+1]=noise
  end
 end
end
end
function Script:Collision(entity, position, normal, speed)
if speed>self.threshhold then
 if #self.sound>0 then
  local collisiontype = entity:GetCollisionType()
  if collisiontype==Collision.Prop or collisiontype==Collision.Scene then
   local t = Time:GetCurrent()
   if t-ImpactNoiseLastSoundTime>self.maxfrequency then
 ImpactNoiseLastSoundTime=t
 local n=math.random(#self.sound)
 local noise = self.sound[n]
 self.entity:EmitSound(noise,self.range)
   end
  end
 end
end
end

Link to comment
Share on other sites

I remember getting this issue before. Two things could be happening (idk what the specifics of tables are):

-An empty table is just a nil entity, so you can't get the length of it

-The table hasn't be registered as a table yet in the code

 

Either way, you should be able to fix this issue by checking to see if the table is nil (instead of its length):

 

function Script:Collision(entity, position, normal, speed)
if speed>self.threshhold then
if self.sound~=nil then
 local collisiontype = entity:GetCollisionType()
 if collisiontype==Collision.Prop or collisiontype==Collision.Scene then
 local t = Time:GetCurrent()
 if t-ImpactNoiseLastSoundTime>self.maxfrequency then
	 ImpactNoiseLastSoundTime=t
	 local n=math.random(#self.sound)
	 local noise = self.sound[n]
	 self.entity:EmitSound(noise,self.range)
 end
end
end
end
end

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...