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

Says this isn't a valid member of Local Script?

I'm in the process of making a sprint script for my Roblox game with a stamina function and everything, but I ran into an error halfway through development of the script. First of all, here's the script I'm using:

if script.Parent.ClassName == ("Model") then
	script.Parent.Humanoid.WalkSpeed = (15)
end

local se = game:GetService("UserInputService")
local hu = script.Parent.Humanoid
local sa = script.Stamina
local st = script.Toggle

function DegenerateStamina()
	st.Value = true
end

function RegenerateStamina()
	st.Value = false
end

se.InputBegan:connect(function(inputObject, gameProcessedEvent)
	if inputObject.KeyCode == Enum.KeyCode.LeftShift then
		hu.WalkSpeed = (25)
		DegenerateStamina()
		repeat wait() until
		se.InputEnded:connect(function(input, gameProcessed)
			if input.KeyCode == Enum.KeyCode.LeftShift then
				hu.WalkSpeed = (15)
				RegenerateStamina()
			end
		end)
	end
end)

The Stamina function isn't finished yet, I just have it set to toggle a value when the character is/isn't sprinting. I have the code in a local script, with two values inside the script: One to keep track of how much stamina the player has (An integer value), and another to detect if the player is sprinting or not so it can decide whether or not to deduct stamina (A bool value). These should all be defined within the script.

Anyways, whenever I try to run the script, I get this error:

15:58:53.836 - Stamina is not a valid member of LocalScript 15:58:53.837 - Stack Begin 15:58:53.837 - Script 'Workspace.EnderGamer358.Sprint', Line 7 15:58:53.838 - Stack End

It's saying that the Stamina value, the one that keeps track of how much stamina my player has, either isn't defined correctly or doesn't exist, even though I checked several times through and thought I got everything perfect. Any errors in my script that I'm just missing?

1 answer

✓ Accepted answer

The reason it happens is most likely because your script tries to index the Values before they are created/replicated to the client.

What you should do is use WaitForChild() instead of indexing them directly.

local sa = script:WaitForChild("Stamina")
local st = script:WaitForChild("Toggle")

Also, you should not connect an event inside another event. Doing that will create a new connection each time they press shift.

I believe you wanted to do something like this:

se.InputBegan:Connect(function(inputObject, gameProcessedEvent)
    if inputObject.KeyCode == Enum.KeyCode.LeftShift then
        hu.WalkSpeed = (25)
        DegenerateStamina()
        repeat wait() until se.InputEnded:Wait().KeyCode == Enum.KeyCode.LeftShift
        hu.WalkSpeed = (15)
        RegenerateStamina()
    end
end)

Btw, you don't need to surround all numbers in (). Also, use Connect() instead of connect().

1 comment

0

Ah, yes. Maybe my brain could have actually worked for once and said "Hey, maybe the reason it isn't accepting the values is because they haven't loaded in yet!" Anyways, thanks for your help!

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