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.
01 | local Plr = game.Players.LocalPlayer |
02 | local Char = Plr.Character |
03 | if not Char or not Char.Parent then |
04 | Char = Plr.CharacterAdded:wait() |
05 | end |
06 |
07 | function 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) |
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:
1 | 19 : 19 : 33.619 - Players.Player 1. 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:
1 | Sound.SoundId = math.random(#Sounds) |
You put Sound.SoundId = math.random[#Sounds]
this makes no sense if you're trying to get the sound id from the table.
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.
Trying adding a random seed Use this...
01 | local Plr = game.Players.LocalPlayer |
02 | local Char = Plr.Character |
03 | if not Char or not Char.Parent then |
04 | Char = Plr.CharacterAdded:wait() |
05 | end |
06 | math.randomseed(tick()) -- this is random seed |
07 |
08 | function 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() |