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

I'm trying to make a part where you touch it boost your speed. How do i get the player?

I try to get the player, but this is all that comes up.

Workspace.speedGiver.speedGiverScript:4: attempt to index local 'playr' (a nil value)

`local Players = game:GetService("Players")
local playr = Players.LocalPlayer
local character = playr.Character
local humanoid = character.Humanoid

local function onTouch()
	
	humanoid.Walkspeed = 22
	
end

onTouch()```

Also, sorry I have no idea how to add the code the right way.

3 answers

The Big Heckin Touched Function

okok so you're not gonna believe this but server scripts are not the same as localscripts. they cannot access the LocalPlayer, nor can they access the PlayerGui (rather, any client-sided descendants of a player.)

first, you should connect to the touched event to the script's parent, like this:

script.Parent.Touched:Connect(function(t)

end);

we place an anonymus function within the connect (which is cool fancy speak for creating the function in the event) and it calls the function within every contact

now, we use :FindFirstAncestorWhichIsA() and Players:GetPlayerFromCharacter(). we use FindFirstAncestorWhichIsA incase you have any nested parts, like so:

script.Parent.Touched:Connect(function(t)
	if (t:FindFirstAncestorWhichIsA('Model')) then
	
	end
end);

we can have two if statements, but we can be cool kids and have ONE! (and also use variables cuz cool kids use those too)

script.Parent.Touched:Connect(function(t)
	local Ancestor = t:FindFirstAncestorWhichIsA('Model');
	if (Ancestor and game:GetService('Players'):GetPlayerFromCharacter(Ancestor)) then
		local Humanoid = Ancestor:FindFirstChild('Humanoid', true)
		if (Humanoid) then
			Humanoid.WalkSpeed = 25;
		end
	end;
end);

and zoom bop bap BOW you got your code thing. if you wish, you can use a table-based debounce to make sure that other people can touch this at once, or you can use a bool-based debounce to make sure only one person can touch this at a time.

woot poot, enjoy stuff!

please upvote if helped thanks :)

4 comments

1

After reading the other two answers from two people that just wanted a free answer to get some quick reputation from a beginners question, this was refreshing to read lmao. I gave you an upvote.

0

Thanks!

0

Thanks. As you can tell I am obviously very experienced and well versed in lua coding, but what is the t for?

0

The "t" is the first argument that touched provides. A lot of signals provide an argument for the function to use. In this case, "t" is the object that touched the script's parent.

You need to use the Touched event with the part you mentioned, then you need to use the GetPlayerFromCharacter() with hit.Parent to get the player.

script inside the part will look like this

script.Parent.Touched:Connect(function(hit)
	pcall(function()
		hit.Parent.Humanoid.Walkspeed = 22
	end)
end)

When something touches it, if it is from the player, then it will find the humanoid and increase the walkspeed. The pcall will deal with any errors given from other parts touching it

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