Here is my localscript code; inside the startergui:
local RemoteEvent = game.Workspace:WaitForChild("Music"):WaitForChild("RemoteEvent") local player = game.Players.LocalPlayer canplay = true RemoteEvent.OnClientEvent:Connect(function() if canplay then local sound = player.PlayerGui:WaitForChild("sound") sound:Play() sound.Looped = true print(otherpart) canplay = false end end)
Here is my script code inside the workspace:
local part = game.Workspace.Music local RemoteEvent = Instance.new("RemoteEvent", part) local sound = game.Workspace["Final Boss"] canplay = true local function muziekspelen(otherpart) print(otherpart) local humanoid = otherpart.Parent:FindFirstChild("Humanoid") if humanoid and canplay then local player = game.Players:GetPlayerFromCharacter(otherpart.Parent) canplay = false RemoteEvent:FireClient(player) sound.Parent = player.PlayerGui sound:Play() sound.Looped = true end canplay = true end part.Touched:connect(muziekspelen)
I don't want to repeat the sound everytime it's being touched by the localplayer. Does anyone know how to do this?
You should call Player:WaitForChild("PlayerGui")
, just because there's no guarantee it's loaded.
Nonetheless, there's no reason to do this server-side at all. You should listen for the BasePart.Touched
event client-side to begin with, instead of wasting bandwith and adding latency.
You can also get rid of the Instance:WaitForChild()
calls for both the music and the RemoteEvent
. Remotes should be stored in a special folder named Network
or Remote
or Remotes
or something of the sort in game.ReplicatedStorage
.