Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What is wrong with my code? It doesn't work. Cdfdfsdfdfsdfdf

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?

2 comments

0

Please clarify your question. What is your code supposed to do? ...Cdfdfsdfdfsdfdf??

0

If a certain block called music get's touced it needs to play a sound that can only be heared by the localplayer i got that working but if u keep walking on the block the sound will just replay

1 answer

✓ Accepted answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.