Script won't trigger

This script won’t trigger, i know that because line 5 doesn’t print anything in the ouput, there is also no errors, code is inside a ServerScript inside a textButton.


plrs.PlayerAdded:Connect(function(player)
	script.Parent.MouseButton1Click:Connect(function(triggered)
	print("lol")
		if player.Character:FindFirstChild("Humanoid") then
			player.Character:WaitForChild("Head"):FindFirstChild("Screaming"):Play()

		end

	end)
end)
1 Like

MouseButton1Click events only work in LocalScripts if I’m not wrong. Try putting a LocalScript instead.

This code is such a bad practice.

Use a LocalScript instead of a ServerScript.
Remove the triggered parameter in MouseButton1Click function, i don’t see that you really need to use that for the code you’re trying to write.
Remove the PlayerAdded part, it is not required.
If you want to define the player so much, write:

local player = game.Players.LocalPlayer

And then, If you want to make the sound heard by everyone create a Remote Event and Fire it.

Receive the information on server and play the audio there.

1 Like

@Z_lqify has a point. Remote events are very useful in a situation like this. Also, why a server script in a text button? Only use local scripts.

1 Like

I take it you have a way set up to move the scream sound to the head all ready is that correct?

Well … if so:

  1. In ReplicatedStorage create a RemoteEvent and name it Screaming
  2. Place script A in StarterGui
  3. Place script B on the workspace or ServerScriptService

Script A … LocalScript

local player = game:GetService'Players'.LocalPlayer
local mouse = player:GetMouse()
local event = game.ReplicatedStorage.Screaming

mouse.Button1Down:connect(function()
	event:FireServer()
end)

Script B … non-LocalScript

local Players = game:GetService("Players")
local event = game.ReplicatedStorage.Screaming

event.OnServerEvent:connect(function()
	print("lol")
	for i, player in pairs(Players:GetPlayers()) do
		if player.Character:FindFirstChild("Humanoid") then
			player.Character:WaitForChild("Head"):FindFirstChild("Screaming"):Play()
		end	
	end
end)

I’m sure you will need to modify this but, this will get you in the ballpark. GL

1 Like
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, message)
        player.Character:FindFirstChild(“Head”):FindFirstChild(“Screaming”):Play()
        print(message)
end

The code above should be in a server script in ServerScriptService.

Your local script in the text button should be written like this:

script.Parent.MouseButton1Click:Connect(function)
        
        game.ReplicatedStorage.RemoteEvent:FireServer(“Lol!”)
end)

I did that but the sound kept glitching. One time it doesn’t play at all, one time it plays half of the sound and one time it plays the whole sound

1 Like

This is whats happening with your script but in this case its happening with the print, the sound does not play at all.

If you made it to the print you did it … now you have to figure out what is wrong with your sound.
Also there is no debounce on the mouse click … Guess I’m not really sure how you plan to use this.
As it is anyone could spam the mouse and everyone would keep screaming.

That is a raw script … it needs some love.
It may be just playing the sound over and over as fast as possible with no debounce or a check if the sound playing has finished before playing it again.

1 Like

What i mean it’s that now its the same issue with my initial script (the script that i showed in the post is not it)

The way you had things set up it would have never worked …
You do need like Jay said to use a RemoteEvent. Both our examples show you how to do that. There is no way around it … you have to learn how to use RemoteEvents to program Roblox … Don’t fear it, it’s easy!

1 Like

I know, i used remote event before but i don’t know how to face the problem of the sound not playing correctly, so i tried doing something else. I’ll try using my old RemoteEvent script as a reference but i don’t know if it’s really possible to fix the initial problem i was encountering.

ok great. You’re making it to the print so your remote is working … That means that is not the problem. Did you add a debounce to the sound call? Are you sure the sound works at all? Are you hearing any sounds?

i fixed it thanks/ some characters so i can post this: print(“Hello World”)

1 Like