So what I'm trying to do is have it to where the player touches a part, and audio will play inside of the player's gui. It all works find, but once started in player it says that line 9 is a nil value. Anyone know how to fix this?
01 | local buttonPressed = false |
02 |
03 | function onTouch(hit) |
04 | if not buttonPressed then |
05 | buttonPressed = true |
06 | if hit.Parent:FindFirstChild( "Humanoid" ) then |
07 | local user = game.Players:GetPlayerFromCharacter(hit.Parent) |
08 | if user then |
09 | local startui = user:FindFirstChild( "PlayerGui" ) |
10 | if startui then |
11 | startui.StartRace:Play() |
12 | end |
13 | end |
14 | end |
15 |
You need to do this from the client. Put a LocalScript in StarterGui, put the StartRace Sound object in the LocalScript.
Get the character object from the LocalPlayer, and check if the object that touched the part is a descendant of it. This will tell you if the character touched it.
01 | local plr = game.Players.LocalPlayer --The Player |
02 | local char = plr.Character or plr.CharacterAdded:Wait() --Character |
03 | local audio = script:WaitForChild( "StartRace" ) --Audio |
04 | local buttonPressed = false |
05 |
06 | function onTouch(hit) |
07 | if not buttonPressed then |
08 | buttonPressed = true |
09 | if hit:IsDescendantOf(char) then --If character touched |
10 | audio:Play() --Play audio |
11 | end |
12 | buttonPressed = false ; |
13 | end |
14 | end |
15 |
16 | --Below is object to touch. |
17 | workspace.StartRace.Touched:Connect(onTouch) --`connect` is deprecated |