Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Can anyone identify what is causing the delay in this script (UserInputerSevice)?

Edit: Silly ole me forgot to add what the 'delay' was. Basically when I hit shift it works perfectly when I release shift it'll delay an inconsistent amount of times before calling resetSpeed(). Sometimes instantly, sometimes after a few seconds, sometimes never.

Edit 2: I've tried a different method of putting the variable for humanoid inside the functions and changing the variable to be

humanoid=player.Character.Humanoid

I'm still receiving the delay and believe the error is in how I used the UserInputerService

So I'm creating a shift to sprint script, it is noteworthy that this is obviously inside a localscript just to clarify. Here is my code for now, I'm receiving no errors and can't find a place to start.

local player=game.Players.LocalPlayer
local plrname=game.Players.LocalPlayer.Name
local humanoid=game.Workspace:WaitForChild(plrname).Humanoid
local input=game:GetService("UserInputService")

local function changeSpeed(player)
	humanoid.WalkSpeed=25
end

local function resetSpeed(player)
	humanoid.WalkSpeed=16
end

input.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		changeSpeed(player)
	else
		resetSpeed(player)
	end
end)

I believe the most possible suspect here is:

local humanoid=game.Workspace:WaitForChild(plrname).Humanoid

The issue is I have attempted using player.Character.Humanoid, as-well as game.Workspace:FindFirstChild(plrname).Humanoid. Both return nil as (i believe) the script is executed before the players spawns in therefor it returns nil.

2 comments

0

local humanoid=game.Players.LocalPlayer.Character.Humanoid

0

I've tried this before, I get attempted to index "character" (a nil value) as I said in the question

1 answer

✓ Accepted answer

Instead of using the event InputBegan for both starting and stopping the sprinting, let's using the event InputEnded to stop the sprinting

let's paste your script and change a few things, lets use the characteradded event of player to get the character, and also put the input events inside of that

local player=game.Players.LocalPlayer
local input=game:GetService("UserInputService")

player.CharacterAdded:connect(function(char)
	local humanoid = char:WaitForChild("Humanoid")
	input.InputBegan:connect(function(input)
		if input.KeyCode == Enum.KeyCode.LeftShift then
			humanoid.WalkSpeed=25
		end
	end)
	input.InputEnded:connect(function(input)
		if input.KeyCode == Enum.KeyCode.LeftShift then
			humanoid.WalkSpeed=16
		end
	end)
end)

so it is setup like this, when the character is added the two input events will check if either shift has been clicked or if it has been lifted. it will change the speed accordingly, in the future don't forget to waitforchild("Humanoid")

1 comment

0

Worked out, quick fix in changing local humanoid variable to a regular variable so .InputEnded can use it as-well.

This discussion is preserved as it was. New answers, comments, and votes are closed.