Answered by
5 years ago Edited 5 years ago
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:
1 | script.Parent.Touched:Connect( function (t) |
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:
1 | script.Parent.Touched:Connect( function (t) |
2 | if (t:FindFirstAncestorWhichIsA( 'Model' )) then |
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)
1 | script.Parent.Touched:Connect( function (t) |
2 | local Ancestor = t:FindFirstAncestorWhichIsA( 'Model' ); |
3 | if (Ancestor and game:GetService( 'Players' ):GetPlayerFromCharacter(Ancestor)) then |
4 | local Humanoid = Ancestor:FindFirstChild( 'Humanoid' , true ) |
6 | Humanoid.WalkSpeed = 25 ; |
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 :)