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

How to access the a player's audio in gui on a touched event?

Asked by 7 years ago

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?

01local buttonPressed = false
02 
03function 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
14end
15 
View all 21 lines...

1 answer

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

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.

01local plr = game.Players.LocalPlayer --The Player
02local char = plr.Character or plr.CharacterAdded:Wait() --Character
03local audio = script:WaitForChild("StartRace") --Audio
04local buttonPressed = false
05 
06function 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
14end
15 
16--Below is object to touch.
17workspace.StartRace.Touched:Connect(onTouch) --`connect` is deprecated
Ad

Answer this question