Can we get the player model that clicked an image button

Hello !
For a sound system, i’m trying to make like, when the player enter the sound ID, it’ll create a sound inside his character so player in a certain range can hear it (not everybody, would be annoying). Is there anything that can handle something like that?

2 Likes

I’m guessing this is a Localscript? If so, you can just use game.Players.LocalPlayer to get the local player and put the sound there.

Or alternatively that could be what is needed for your thing, make a RemoteEvent that adds the sound with the specified sound id into the character, which certainly is possible, all you need is a player and soundId parameter in the OnServerEvent, and when firing the Remote, pass in the sound id

Isn’t game.Players.LocalPlayer supposed to get the player instance, not the model?

If you want the character from the localplayer, just do game.Players.LocalPlayer.Character

If OP plans on turning on SoundService.RespectFilteringEnabled then they will need to use a RemoteEvent. You’ll know the player because it’s the first parameter of a remote event. Then just put a sound in their character and that’s done.

See Sound.EmitterSize.

EDIT: apparently EmitterSize is depreciated, see Sound.RollOffMaxDistance.

You need remote events and have the event fire to the server and back to the local player’s scripts

Since I don’t want to rewrite the whole code, I’m going to do it with server script (anyways, it’s going to be for a gamepass so I don’t really care). Anything can do like game.Players.LocalPlayer.Character with server thing?

It depends on how your system works, There are many ways you can get a certain player’s character. What are you trying to do exactly?

Like the player click the button, and the code will create an instance “Sound” in the humanoidrootpart of the player who entered the id and clicked the button

Then you can do what I previously stated, make a RemoteEvent with parameters for the player and soundid and fire over the soundid and make some code to put a sound instance in their character

Ye but Everything is in a server script

What type of button is it, Gui Button?

I’m using a ScreenGUI that only the player who bought the gamepass will have access

I made it client side, got this script :

local mps = game:GetService("MarketplaceService")


local bg = script.Parent

local playBtn = bg:WaitForChild("PlayButton")
local loopingBtn = bg:WaitForChild("LoopingToggle")
local timePos = bg:WaitForChild("MusicTimePos")

local volumeToggleBtn = bg:WaitForChild("VolumeToggle")
local volumeUp = bg:WaitForChild("VolumeUp")
local volumeDown = bg:WaitForChild("VolumeDown")
local volumeAmount = bg:WaitForChild("VolumeAmount")

local musicName = bg:WaitForChild("MusicName")
local musicOwner = bg:WaitForChild("MusicOwner")

local musicInputBox = bg:WaitForChild("MusicInput")

local music = Instance.new('Sound')
music.Name = "Music"
music.RollOffMaxDistance = 50
music.RollOffMode = Enum.RollOffMode.Linear
music.Parent = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")


local isLooping = false
local isPlaying = false
local isMuted = false

local currentVolume = 10
local minVolume = 0
local maxVolume = 10


musicName.Text = ""
musicOwner.Text = ""
volumeAmount.Text = "10"
timePos.Text = "00:00"


musicInputBox.FocusLost:Connect(function()
	
	local input = musicInputBox.Text
	local success, info = pcall(mps.GetProductInfo, mps, input)
	
	if success and info.AssetTypeId == 3 then
		
		music:Stop()
		isPlaying = false	
		playBtn.Image = game.ReplicatedStorage.Decals.Play.Texture
		
		music.SoundId = "rbxassetid://" .. input
		
		musicName.Text = info.Name
		musicOwner.Text = " by " .. info.Creator.Name
	end
end)

playBtn.MouseButton1Click:Connect(function()
	
	if isPlaying then
		isPlaying = false
		
		playBtn.Image = game.ReplicatedStorage.Decals.Play.Texture
		music:Pause()
		
	else
		isPlaying = true
		
		playBtn.Image = game.ReplicatedStorage.Decals.Pause.Texture
		music:Resume()
	end
end)

loopingBtn.MouseButton1Click:Connect(function()
	
	if isLooping then
		isLooping = false
		
		music.Looped = false
		
		loopingBtn.ImageColor3 = Color3.fromRGB(95, 95, 95)
		
	else
		
		isLooping = true
		
		music.Looped = true
		
		loopingBtn.ImageColor3 = Color3.fromRGB(255, 255, 255)
	end
end)

volumeToggleBtn.MouseButton1Click:Connect(function()
	
	if isMuted then 
		isMuted = false
		
		volumeToggleBtn.ImageColor3 = Color3.fromRGB(95, 95, 95)
		
		music.Volume = currentVolume / 10
		
	else
		isMuted = true
		
		volumeToggleBtn.ImageColor3 = Color3.fromRGB(255, 255, 255)
		
		music.Volume = 0
	end
end)

volumeUp.MouseButton1Click:Connect(function()
	
	isMuted = false
	volumeToggleBtn.ImageColor3 = Color3.fromRGB(95, 95, 95)
	
	currentVolume = math.clamp(currentVolume + 1, minVolume, maxVolume)
	
	music.Volume = currentVolume / 10
	volumeAmount.Text = currentVolume
end)
volumeDown.MouseButton1Click:Connect(function()
	
	isMuted = false
	volumeToggleBtn.ImageColor3 = Color3.fromRGB(95, 95, 95)
	
	currentVolume = math.clamp(currentVolume - 1, minVolume, maxVolume)
	
	music.Volume = currentVolume / 10
	volumeAmount.Text = currentVolume
end)

game:GetService("RunService").RenderStepped:Connect(function()
	
	local musicTimePos = math.floor(music.TimePosition)
	
	local musicTimePosSecs = musicTimePos % 60
	local musicTimePosMins = math.floor(musicTimePos / 60)
	
	if string.len(musicTimePosSecs) < 2 then musicTimePosSecs = "0" .. musicTimePosSecs end
	if string.len(musicTimePosMins) < 2 then musicTimePosMins = "0" .. musicTimePosMins end
	
	timePos.Text = musicTimePosMins .. ":" .. musicTimePosSecs
end)

But i just don’t know how to play the music for the server (how to transfer the player name, and the music via a remote event)

If you made this script in client side you can do what @EmbatTheHybrid said, without needing to make a remote event,

My goal is to make like the server can hear the music, but o take the player model, I need to use local script

You can still use a RemoteEvent to do what is needed, you ust fire in the SoundId and the ServerEvent will already give you the player instance, you just need to include the soundid, if your custom character is in the Character property, then it should find it

local isLooping = false
local isPlaying = false
local isMuted = false

local currentVolume = 10
local minVolume = 0
local maxVolume = 10
local event = game.ReplicatedStorage.REMusic


event.MusicBoard.OnServerEvent:Connect(function(char, input)
	local music = char:FindFirstChild("HumanoidRootPart")
	music.SoundId = "rbxassetid://" .. input
end)

game.ReplicatedStorage.REMusic.Play.OnServerEvent:Connect(function(char)
	local music = char.HumanoidRootPart.Music
	if isPlaying then
		isPlaying = false
		music:Pause()
	else
		isPlaying = true
		music:Resume()
	end
end)

game.ReplicatedStorage.REMusic.Loop.OnServerEvent:Connect(function(char)
	local music = char.HumanoidRootPart.Music

	if isLooping then
		isLooping = false
		music.Looped = false
	else
		isLooping = true
		music.Looped = true
	end
end)

game.ReplicatedStorage.REMusic.Mute.OnServerEvent:Connect(function(char)
	local music = char.HumanoidRootPart.Music

	if isMuted then 
		isMuted = false
		music.Volume = currentVolume / 10
	else
		isMuted = true
		music.Volume = 0
	end
end)

game.ReplicatedStorage.REMusic.UpVolume.OnServerEvent:Connect(function(char)
	local music = char.HumanoidRootPart.Music
	isMuted = false
	currentVolume = math.clamp(currentVolume + 1, minVolume, maxVolume)
	music.Volume = currentVolume / 10
end)

game.ReplicatedStorage.REMusic.LowerVolume.OnServerEvent:Connect(function(char)
	local music = char.HumanoidRootPart.Music
	isMuted = false
	currentVolume = math.clamp(currentVolume - 1, minVolume, maxVolume)
	music.Volume = currentVolume / 10
end)

I made this script on server side, but when I start it, it says :
HumanoidRootPart is not a valid member of Player
attempt to concatenate string with Instance

The thign that is given is the player instance, not the character, so you have to do char.Character, which a check to see if the character exists firstly. Also how are you firing the RemoteEvent? Are you including the player?

This sit he script that handle firing :

local mps = game:GetService("MarketplaceService")


local bg = script.Parent

local playBtn = bg:WaitForChild("PlayButton")
local loopingBtn = bg:WaitForChild("LoopingToggle")
local timePos = bg:WaitForChild("MusicTimePos")

local volumeToggleBtn = bg:WaitForChild("VolumeToggle")
local volumeUp = bg:WaitForChild("VolumeUp")
local volumeDown = bg:WaitForChild("VolumeDown")
local volumeAmount = bg:WaitForChild("VolumeAmount")

local musicName = bg:WaitForChild("MusicName")
local musicOwner = bg:WaitForChild("MusicOwner")

local musicInputBox = bg:WaitForChild("MusicInput")

local char = game.Players.LocalPlayer.Character
local music = Instance.new('Sound')
music.Name = "Music"
music.RollOffMaxDistance = 50
music.RollOffMode = Enum.RollOffMode.Linear
music.Parent = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local grp = game.ReplicatedStorage.REMusic

local isLooping = false
local isPlaying = false
local isMuted = false

local currentVolume = 10
local minVolume = 0
local maxVolume = 10


musicName.Text = ""
musicOwner.Text = ""
volumeAmount.Text = "10"
timePos.Text = "00:00"
local event = game.ReplicatedStorage.REMusic.MusicBoard

musicInputBox.FocusLost:Connect(function()
	
	local input = musicInputBox.Text
	local success, info = pcall(mps.GetProductInfo, mps, input)
	
	if success and info.AssetTypeId == 3 then
		
		event:FireServer(char, input)
		isPlaying = false	
		playBtn.Image = game.ReplicatedStorage.Decals.Play.Texture
		musicName.Text = info.Name
		musicOwner.Text = " by " .. info.Creator.Name
	end
end)

playBtn.MouseButton1Click:Connect(function()
	
	game.ReplicatedStorage.REMusic.Play:FireServer(char)
	if isPlaying then
		isPlaying = false
		playBtn.Image = game.ReplicatedStorage.Decals.Play.Texture		
	else
		isPlaying = true
		playBtn.Image = game.ReplicatedStorage.Decals.Pause.Texture
	end
end)

loopingBtn.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.REMusic.Loop:FireServer(char)
	if isLooping then
		isLooping = false		
		loopingBtn.ImageColor3 = Color3.fromRGB(95, 95, 95)
	else
		isLooping = true		
		loopingBtn.ImageColor3 = Color3.fromRGB(255, 255, 255)
	end
end)

volumeToggleBtn.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.REMusic.Mute:FireServer(char)
	if isMuted then 
		isMuted = false
		volumeToggleBtn.ImageColor3 = Color3.fromRGB(95, 95, 95)
	else
		isMuted = true
		volumeToggleBtn.ImageColor3 = Color3.fromRGB(255, 255, 255)
		music.Volume = 0
	end
end)

volumeUp.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.REMusic.UpVolume:FireServer(char)
	isMuted = false
	volumeToggleBtn.ImageColor3 = Color3.fromRGB(95, 95, 95)
	volumeAmount.Text = currentVolume
end)

volumeDown.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.REMusic.LowerVolume:FireServer(char)
	isMuted = false
	volumeToggleBtn.ImageColor3 = Color3.fromRGB(95, 95, 95)
	volumeAmount.Text = currentVolume
end)

game:GetService("RunService").RenderStepped:Connect(function()
	
	local musicTimePos = math.floor(music.TimePosition)
	
	local musicTimePosSecs = musicTimePos % 60
	local musicTimePosMins = math.floor(musicTimePos / 60)
	
	if string.len(musicTimePosSecs) < 2 then musicTimePosSecs = "0" .. musicTimePosSecs end
	if string.len(musicTimePosMins) < 2 then musicTimePosMins = "0" .. musicTimePosMins end
	
	timePos.Text = musicTimePosMins .. ":" .. musicTimePosSecs
end)