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
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.
Thanks!
Thanks. As you can tell I am obviously very experienced and well versed in lua coding, but what is the t for?
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.