Whistle script only playing on client

The script itself works flawlessly, however, the sound only plays on the client, not in the server. This is a problem because it makes the script (which purpose is for locating other players) entirely useless


local Player = script.parent

local function playSound()
	local sound = Instance.new("Sound")
	sound.SoundId = "rbxassetid://9120675904"
	sound.Parent = game.Workspace
	sound:Play()
end

local onCooldown = false
local cooldownTime = 2
game:GetService("UserInputService").InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.T and not onCooldown then
		onCooldown = true
		playSound()
		task.delay(cooldownTime, function()
			onCooldown = false
		end)	
	end
end)

Duh. RemoteEvent | Documentation - Roblox Creator Hub

What exactly am I changing? I am a beginner in scripting and not exactly sure what to do.

Try this or something along these lines.

LocalScript:


local Player = script.parent

local function playSound()
	game.ReplicatedStorage.WhistleEvent:FireServer(Player)
end

local onCooldown = false
local cooldownTime = 2
game:GetService("UserInputService").InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.T and not onCooldown then
		onCooldown = true
		playSound()
		task.delay(cooldownTime, function()
			onCooldown = false
		end)	
	end

ServerScript:

game.ReplicatedStorage.WhistleEvent.OnServerEvent:Connect(function(Player)
  if not Player.Character then
    repeat wait() until Player.Character
  end

 local clone = script.WhistleSound:Clone()
 clone.Parent = Player.Character.Head
 clone:Play()
 clone.Stopped:Wait() -- this might not be correct. event names for when things stop are a lil uneven.
 clone:Destroy()
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.