Does anyone know a good way to get the humanoid through a variable?
If you’re trying to access the humanoid in a local script you could do:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character.Humanoid
2 Likes
Not in a local script, just a regular script
If you’re trying to access the humanoid in a server script there are several different scenarios you can run into. But here’s one way:
local Players = game:GetService("Players")
local function OnPlayerAdded(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character.Humanoid
-- code here
end)
end
Players.PlayerAdded:Connect(OnPlayerAdded)
-- if the player joins before the signal can connect, here's a fail safe
for _, player in pairs(Players:GetPlayers()) do
OnPlayerAdded(player)
end
It really depends what you’re trying to do. Hard to give you an example when we don’t know how you’re going to be using the variable.
1 Like
I ran this script and it said “Walkspeed is not a member of humanoid”
local Players = game:GetService("Players")
local function OnPlayerAdded(player)
player.CharacterAdded:Connect(function(character)
local humanoid = character.Humanoid
local speed = Instance.new("IntValue",player)
speed.Name = player.Name
speed.Value = 50
humanoid.Walkspeed = speed.Value
end)
end
Players.PlayerAdded:Connect(OnPlayerAdded)
You need to change
humanoid.Walkspeed = speed.Value
to
humanoid.WalkSpeed = speed.Value
and it should work fine with you
How is mine not the solution to the topic’s question?
3 Likes