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

Detecting Humanoid and Making a sound doesn't work?

Asked by 7 years ago

Hello, I'm just wondering why this script won't work (A local script inside starterGui) there is no ouput by the way.

Please support me with any solutions, thanks.

01local Plr = game.Players.LocalPlayer
02local Char = Plr.Character
03if not Char or not Char.Parent then
04    Char = Plr.CharacterAdded:wait()
05end
06 
07function PlaySound()
08    local Sounds = {"1470936712", "1470937053", "1470937969"}
09    wait()
10    if Char.Humanoid.Health < 100 then
11        local Sound = Instance.new('Sound')
12        Sound.SoundId = math.random[#Sounds]
13        Sound.Parent = Char:WaitForChild('Head')
14        Sound:Play()
15        wait(Sound.TimeLength)
View all 25 lines...
0
i think you need the rbxassetid:// prefix in the SoundId. You also need to put Sounds[math.random(#Sounds)] on line 12. RubenKan 3615 — 7y

3 answers

Log in to vote
0
Answered by
sigve10 94
7 years ago

So, I put your script into an empty baseplate and started making my character take damage. Unlike what you said, it did output an error about line 12:

119:19:33.619 - Players.Player1.PlayerGui.LocalScript:12: attempt to index field 'random' (a function value)

The reason for the problem that you are stumbling upon is the fact that you are using square brackets instead of round brackets for math.random. Remember that math.random is a function and not a table. Round brackets () are used to contain parameters for functions or in mathematical equations, and square brackets [] are used to identify an item in a table.

To fix line 12, simply replace the square brackets with round brackets, like so:

1Sound.SoundId = math.random(#Sounds)
Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago
  1. You put Sound.SoundId = math.random[#Sounds] this makes no sense if you're trying to get the sound id from the table.

  2. Try using print functions to see where your error is.

also I suggest using a while true do if you want your sound to repeat every time the player's health goes down or something.

Log in to vote
0
Answered by 7 years ago

Trying adding a random seed Use this...

01local Plr = game.Players.LocalPlayer
02local Char = Plr.Character
03if not Char or not Char.Parent then
04    Char = Plr.CharacterAdded:wait()
05end
06math.randomseed(tick()) -- this is random seed
07 
08function PlaySound()
09    local Sounds = {"1470936712", "1470937053", "1470937969"}
10    wait()
11    if Char.Humanoid.Health < 100 then
12        local Sound = Instance.new('Sound')
13        Sound.SoundId = math.random[#Sounds]
14        Sound.Parent = Char:WaitForChild('Head')
15        Sound:Play()
View all 26 lines...

Answer this question