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

Trying to make a script that adds different instances into players when they join?

Asked by 6 years ago
local Players = game:GetService("Players")
local audio = game.ServerStorage.Items.Effects.Audio.Sound
local particle1 = game.ServerStorage.Items.Effects.Particles.ParticleEmitter
local particle2 = game.ServerStorage.Items.Effects.Particles.ParticleEmitter2
local velocity = game.ServerStorage.Items.Effects.Velocity.BodyVelocity

function onPlayerAdded(player)
    audio:Clone()
    audio.Parent = game.Players
end

Players.PlayerAdded:connect(onPlayerAdded)

for _,player in pairs(Players:GetPlayers()) do
     onPlayerAdded(player)
end

What I want it to do is to move different classes/instances from lighting or serverstorage to a player when they join/re spawn but they don't seem to work.

1 answer

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

Cloning

Instead of storing the cloned audio in a variable, you don't. If you don't use this, you can't reference the cloned object

Deprecated Syntaxes

You are using :connect(). This is deprecated or not recommended to use. Use :Connect()

Why Are You Looping The Players?

You are looping the players even though you have made the onPlayerAdded() to listen for the PlayerAdded event.

The PlayerAdded event already has its own argument, the player, so, you don't need to call the function again. Even though the player resets, it will still work.

Here is the fix:

--\\ Services
local Players = game:GetService("Players")
local serverStorage = game:GetService("ServerStorage")
--\\ Cloned Objects
local items = serverStorage:WaitForChild("Items")
local effects = items:WaitForChild("Effects")
local sounds = effects:WaitForChild("Audio")
local audio = audio:WaitForChild("Sound")

local particles = effects:WaitForChild("Particles")
local particle1 = particles:WaitForChild("ParticleEmitter")
local particle2 = particles:WaitForChild("ParticleEmitter2")

local velocityFolder = effects:WaitForChild("Velocity")
local velocity = velocityFolder:WaitForChild("BodyVelocity")

--\\ Clone Sound onPlayerAdded()
function onPlayerAdded(player)
    local newAudio = audio:Clone()
    newAudio.Parent = Players
end

Players.PlayerAdded:Connect(onPlayerAdded)
0
look below LoganboyInCO 150 — 6y
Ad

Answer this question