How to get a player's physical body in a localscript?

So today I was wondering how I would find a player’s physical body (possible through a variable)

I’ve seen how to get their information from the players folder, but I couldn’t find how to get the physical body in Workspace

Here’s my LocalScript code:

local remoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local playerPlayerList = game:GetService("Players").LocalPlayer
print(playerPlayerList)
local playerPlayerListName = playerPlayerList.Name

local function changeSounds (usedSounds)
		print("Remote Event Fired. Setting IDs...")
		local player = game:GetService("Workspace").
		print(usedSounds[1])
		local sounds = player:WaitForChild("HumanoidRootPart")
		sounds.Running.SoundId="rbxassetid://" .. usedSounds[1]
		print("Running Sound Changed to " .. usedSounds[1])
		sounds.Died.SoundId="rbxassetid://" .. usedSounds[2]
		sounds.Climbing.SoundId="rbxassetid://" .. usedSounds[3]
		sounds.FreeFalling.SoundId="rbxassetid://" .. usedSounds[4]
		sounds.GettingUp.SoundId="rbxassetid://" .. usedSounds[5]
		sounds.Jumping.SoundId="rbxassetid://" .. usedSounds[6]
		sounds.Landing.SoundId="rbxassetid://" .. usedSounds[7]
		sounds.Splash.SoundId="rbxassetid://" .. usedSounds[8]
		sounds.Swimming.SoundId="rbxassetid://" .. usedSounds[9]
	end
	remoteEvent.OnClientEvent:Connect(changeSounds)
	
1 Like

@Aeventy
I believe so, although I still seem to have an error printing.
Here’s the updated script:

local remoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local character = player.Character

local function changeSounds (usedSounds)
	print("Remote Event Fired. Setting IDs...")
	print(usedSounds[1])
	print(character.Name)
	character.HumanoidRootPart.Running.SoundId="rbxassetid://" .. usedSounds[1]
	print("Running Sound Changed to " .. usedSounds[1])
	character.HumanoidRootPart.Died.SoundId="rbxassetid://" .. usedSounds[2]
	character.HumanoidRootPart.Climbing.SoundId="rbxassetid://" .. usedSounds[3]
	character.HumanoidRootPart.FreeFalling.SoundId="rbxassetid://" .. usedSounds[4]
	character.HumanoidRootPart.GettingUp.SoundId="rbxassetid://" .. usedSounds[5]
	character.HumanoidRootPart.Jumping.SoundId="rbxassetid://" .. usedSounds[6]
	character.HumanoidRootPart.Landing.SoundId="rbxassetid://" .. usedSounds[7]
	character.HumanoidRootPart.Splash.SoundId="rbxassetid://" .. usedSounds[8]
	character.HumanoidRootPart.Swimming.SoundId="rbxassetid://" .. usedSounds[9]
end
remoteEvent.OnClientEvent:Connect(changeSounds)

print(character.Name)

is causing the error 10:04:33.900 - Players.Derpee_Kirbee.PlayerScripts.LocalScript:9: attempt to index nil with 'Name' to pop up.

It’s possible that “character” is being referenced before it’s actually created.

Replace line 4 to local character = player.CharacterAdded:Wait()

Well, also instead of repeating this 8 times:

character.HumanoidRootPart

Make a local:

local humroot = character.HumanoidRootPart

And then:

	humroot.Died.SoundId="rbxassetid://" .. usedSounds[2]
	humroot.Climbing.SoundId="rbxassetid://" .. usedSounds[3]
	humroot.FreeFalling.SoundId="rbxassetid://" .. usedSounds[4]
	humroot.GettingUp.SoundId="rbxassetid://" .. usedSounds[5]
	humroot.Jumping.SoundId="rbxassetid://" .. usedSounds[6]
	humroot.Landing.SoundId="rbxassetid://" .. usedSounds[7]
	humroot.Splash.SoundId="rbxassetid://" .. usedSounds[8]
	humroot.Swimming.SoundId="rbxassetid://" .. usedSounds[9]
1 Like

Okay I printed the character’s name and it’s correct now thanks. But why is
character.HumanoidRootPart.Running.SoundId="rbxassetid://" .. usedSounds[1]
still not functioning?

usedSounds is an array from my serverscript, which is being passed over. I know this is functional bc I can print (usedSounds[1]) and it will print the variable I have inserted in the other script. The array is below:

local usedSounds = {
		RunningSoundId,
		DiedSoundId,
		ClimbingSoundId,
		FreeFallingSoundId,
		GettingUpSoundId,
		JumpingSoundId,
		LandingSoundId,
		SplashSoundId,
		SwimmingSoundId
	}

The variables in the array are listed above, with the first one changed.
the variables are:

local Running = 5721510093
	local Died = 0
	local Climbing = 0
	local FreeFalling = 0
	local GettingUp = 0
	local Jumping = 0
	local Landing = 0
	local Splash = 0
	local Swimming = 0

I don’t quite understand what you mean by

but every sound iD inserted is supposed to be a number if I am understanding you correctly.

All of them except for the first are zero

I don’t think “0” is a valid ID.

You only have a running animation, just use that?

The problem isn’t that they all need IDs. It doesn’t even get to the rest of the numbers in the localscript bc of the error. Also, 0 would be fine, but nil would not work. 0 just doesn’t change the number, I programmed it that way:

for x=1, table.getn(setVariables), 1 do 
		if setVariables[x] ~= 0 then
			usedSounds[x]=setVariables[x]
			print("Sound Number " .. x .. " Was Changed by Script to Sound Id " .. usedSounds[x])
		else
			print("Sound Number " .. x .. " Will Stay as Default Sound")
			print(setVariables[x])
		end
	end

The running is the only one I am testing, it doesn’t get any further. I’ll try the others when the first one works.

Alright well thanks for trying let me see if I can figure it out myself based on the errors now.

change the

part to:

local character = workspace:WaitForChild(player.Name)

I already told him how to find that.
And it’s

Player.Character

I guess yours could work, too.

Sorry I was writing a reply before reading all the replys

@Gusshiy @Aeventy
Here’s the one error that’s popping up now:
HumanoidRootPart is not a valid member of Model "Workspace.Derpee_Kirbee"
Here’s the script:

local remoteEvent = game.ReplicatedStorage:WaitForChild("RemoteEvent")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = game.Players.LocalPlayer
local character = player.CharacterAdded:wait()

local function changeSounds (usedSounds)
	print("Remote Event Fired. Setting IDs...")
	print(usedSounds[1])
	print(player.Name)
	print(character.Name)
	print(usedSounds[1])
	character.HumanoidRootPart.Running.SoundId="rbxassetid://" .. usedSounds[1]
	print("Running Sound Changed to " .. usedSounds[1])
	character.HumanoidRootPart.Died.SoundId="rbxassetid://" .. usedSounds[2]
	character.HumanoidRootPart.Climbing.SoundId="rbxassetid://" .. usedSounds[3]
	character.HumanoidRootPart.FreeFalling.SoundId="rbxassetid://" .. usedSounds[4]
	character.HumanoidRootPart.GettingUp.SoundId="rbxassetid://" .. usedSounds[5]
	character.HumanoidRootPart.Jumping.SoundId="rbxassetid://" .. usedSounds[6]
	character.HumanoidRootPart.Landing.SoundId="rbxassetid://" .. usedSounds[7]
	character.HumanoidRootPart.Splash.SoundId="rbxassetid://" .. usedSounds[8]
	character.HumanoidRootPart.Swimming.SoundId="rbxassetid://" .. usedSounds[9]
end
remoteEvent.OnClientEvent:Connect(changeSounds)

So the error is appearing on line 12, or
character.HumanoidRootPart.Running.SoundId="rbxassetid://" .. usedSounds[1]

When is the RemoteEvent Firing?

@Gusshiy at the end of a server script:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")
--[[
The SoundIds that can be assigned. When changing, use example:
local example = "rbxassetid://5721510093"
When adding your own sounds, add "id" to the end of "rbxasset"

--]]

Players.PlayerAdded:Connect(function(player)
	
	--These are the variables you change
	local Running = 5721510093
	local Died = 0
	local Climbing = 0
	local FreeFalling = 0
	local GettingUp = 0
	local Jumping = 0
	local Landing = 0
	local Splash = 0
	local Swimming = 0
	
	local RunningSoundId = "rbxasset://sounds/action_footsteps_plastic.mp3"
	local DiedSoundId = "rbxasset://sounds/uuhhh.mp3"
	local ClimbingSoundId = "rbxasset://sounds/action_footsteps_plastic.mp3"
	local FreeFallingSoundId = "rbxasset://sounds/action_falling.mp3"
	local GettingUpSoundId = "rbxasset://sounds/action_get_up.mp3"
	local JumpingSoundId = "rbxasset://sounds/action_jump.mp3"
	local LandingSoundId = "rbxasset://sounds/action_jump_land.mp3"
	local SplashSoundId = "rbxasset://sounds/impact_water.mp3"
	local SwimmingSoundId = "rbxasset://sounds/action_swim.mp3"
	
	
	
	local setVariables = {
		Running,
		Died,
		Climbing,
		FreeFalling,
		GettingUp,
		Jumping,
		Landing,
		Splash,
		Swimming
	}
	
	local usedSounds = {
		RunningSoundId,
		DiedSoundId,
		ClimbingSoundId,
		FreeFallingSoundId,
		GettingUpSoundId,
		JumpingSoundId,
		LandingSoundId,
		SplashSoundId,
		SwimmingSoundId
	}
	
	print("Running sound is " .. setVariables[1])
	print(table.getn(setVariables))
	for x=1, table.getn(setVariables), 1 do 
		if setVariables[x] ~= 0 then
			usedSounds[x]=setVariables[x]
			print("Sound Number " .. x .. " Was Changed by Script to Sound Id " .. usedSounds[x])
		else
			print("Sound Number " .. x .. " Will Stay as Default Sound")
			print(setVariables[x])
		end
	end

	remoteEvent:FireClient(player, usedSounds)
	
end)

You might want to put in a workspace:WaitForChild(player.Name) at the start of the PlayerAdded event. It may be firing before the HumanoidRootPart is loaded

@Gusshiy you mean the PlayerAdded in the ServerScript?

@Derpee_Kirbee yes. Sorry I wasn’t clear enough

@Gusshiy but there’s no “player” defined in the server script