Help referencing a player's humanoid in a script

Hey,
so Iv’e developed a game with boosts, 2 in particular are jumppower and walkspeed boosts. Whenever a player enables it, I scripted it to work but, if they die, I wanted the boost to be activated still when they respawn.

I tried in a script, trying to give the player their current walkspeed/jumppower boost whenever their character is added, also using RemoteEvents.

Gui button for boost.
Local Script:

local buy = script.Parent
local Enable = “Enable”
local Disable = “Disable”

local function Boost()
if buy.Text == Enable then
buy.BackgroundColor3 = Color3.new(255,85,0)
buy.Text = Disable
game.ReplicatedStorage.WalkspeedAndJumppower.Walkspeed1:FireServer()
elseif buy.Text == Disable then
buy.BackgroundColor3 = Color3.new(0,255,0)
buy.Text = Enable
game.ReplicatedStorage.WalkspeedAndJumppower.NoWalkspeed1:FireServer()
end
end

buy.MouseButton1Click:Connect(Boost)

game.Players.LocalPlayer.CharacterAdded:Connect(function()
if script.Parent.Text == Disable then
game.ReplicatedStorage.WalkspeedAndJumppower.Walkspeed1:FireServer()
end
end)

I put this script in StarterCharacterScripts and did script.parent.Humanoid to reference a player’s humanoid, doing it any other way just didn’t work.:arrow_heading_down:

Script:

local function Walkspeed1()
script.Parent.Humanoid.WalkSpeed = script.Parent.Humanoid.WalkSpeed +10
end

game.ReplicatedStorage.WalkspeedAndJumppower.Walkspeed1:OnServerEvent:Connect(Walkspeed1)

It works but, it affects all Players, I don’t know how make it so that it affects the player activating the boost.

1 Like

You could just reference your ServerScript inside ServerScriptService, since StarterCharacterScripts would fire for all clients when it gets received by the Event

Another thing as well, but the Player is the first parameter for OnServerEvent as well so you don’t need to worry on putting it there :wink:

--Put this inside ServerScriptService

local function Walkspeed1(Player)
    Player.Character.Humanoid.WalkSpeed += 10
end

game.ReplicatedStorage.WalkspeedAndJumppower.Walkspeed1:OnServerEvent:Connect(Walkspeed1)
1 Like

Any reason you’re not setting the walkspeed locally on the client?

local buy = script.Parent
local Enable = "Enable"
local Disable = "Disable"

-- get current humanoid from localplayer
local LocalPlayer = game:GetService'Players'.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild'Humanoid'

local function Boost()
	if buy.Text == Enable then
		buy.BackgroundColor3 = Color3.new(255,85,0)
		buy.Text = Disable
		Humanoid.WalkSpeed = Humanoid.WalkSpeed + 10
	elseif buy.Text == Disable then
		buy.BackgroundColor3 = Color3.new(0,255,0)
		buy.Text = Enable
		Humanoid.WalkSpeed = Humanoid.WalkSpeed - 10
	end
end

buy.MouseButton1Click:Connect(Boost)

LocalPlayer.CharacterAdded:Connect(function(NewCharacter)
	-- when localplayer respawns, override old character and humanoid variables with new ones
	Character = NewCharacter
	Humanoid = Character:WaitForChild'Humanoid'
	if script.Parent.Text == Disable then
		Humanoid.WalkSpeed = Humanoid.WalkSpeed + 10
	end
end)
1 Like

Ohh, I haven’t done this because I didn’t know, thank you this is very helpful👍.