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

How can i make this script to clone Audio from lighting to players torso?

Asked by 7 years ago
Edited 7 years ago
local sp = script.Parent

sp.Touched:connect(function(hit)
 game.Players:GetPlayerFromCharacter(hit.Parent).PlayerGui:ClearAllChildren()
     local gui = game.Lighting.Chase:Clone()    
    gui.Parent = game.Workspace.player.Character.Torso

 end)

I had trouble trying to make this script clone the "Chase" audio into the torso of player that touched parent of the script. I want to know how can I make that to work. (The script above is my attemp of trying to make it work)

0
p.s. This script must work with Filtering Enabled Unity_456 47 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

Hey Alexeeii,

I have found the issue in your script however, I'm sort of confused about some things. First of all, I am not sure as to why you are clearing all the children under PlayerGui. Some of them are quiet important and are pre-loaded for your Player's needs. Also, you tried to set the parent of the Gui by searching for player in workspace however, you never defined that as the variable or anything of that sort. That won't find the player in the workspace that touched the part. For that you need to use the :GetPlayerFromCharacter(Character) method and set that as the variable.

There's quiet a lot of things wrong with your code and some that confuse me so, I'll just go over the code that I personally prepared which explains to you step by step what each line of code does and I think that it's pretty efficient so I'll show you that.

local part = script.Parent; -- The part that will be touched.
local players = game:GetService("Players"); -- The Players Service
local lighting = game:GetService("Lighting"); -- The Lighting Service
local deb = false; -- Debounce needed for cool down. (We don't want to make 3 Audios in Torso at one touch.)
local cooldown = 2; -- The amount of seconds the cool down will be.

part.Touched:Connect(function(hit) -- Anonymous function.
    local hum = hit.Parent:FindFirstChild("Humanoid"); -- Humanoid variable (If there is one.)
    if hum and not deb then -- Checks if there is a humanoid and if debounce is false.
        deb = true; -- Sets debounce to true so that it doesn't run this function until debounce is set back to false.
        local char = hit.Parent; -- Variable for the Character.
        local player = players:GetPlayerFromCharacter(char); -- Variable for the Player.
        local music = lighting:WaitForChild("Chase"):Clone(); -- Variable for the audio in Lighting called "Chase".
        local torso = char:FindFirstChild("Torso") or char:WaitForChild("UpperTorso"); -- Variable for the Torso.

        music.Parent = torso; -- Sets the Parent of the music to the Torso.

        wait(cooldown) -- Does the cool down.
        deb = false; -- Sets debounce back to false so that the code can be run again.
    end -- end for the if statement
end) -- end for the anonymous function.

Well, I hope I helped and thank you for your time.

~~ KingLoneCat

0
The script doesnt seem to work in studio. Unity_456 47 — 7y
0
I have tried it and it doesnt put the audio into the player's torso Unity_456 47 — 7y
0
Are you sure the audio is named "Chase" and it's in Lighting? KingLoneCat 2642 — 7y
0
Yes. Unity_456 47 — 7y
View all comments (3 more)
0
Wait, It worked only when i removed the admin script from the workspace. Unity_456 47 — 7y
0
Thank you for the help! Unity_456 47 — 7y
0
No problem. KingLoneCat 2642 — 7y
Ad

Answer this question