As of right now, I am currently trying to figure out for my game how I can get the name of the last player to touch an object before that object hits another certain object.
I tried setting up a string value in the object that the players are supposed to touch, and I have a script that sets the object that hits the ball (thats the object), as the string value.
local ball = script.Parent
local lastTouched = script.Parent.LastTouched
ball.Touched:Connect(function(hit)
lastTouched.Value = hit.Name
end)
The line lastTouched.Value = hit.Name doesn’t actually get the player that touched the ball part, it just gets the name of the part that initiated the collision (e.g. a character’s limb.) This is how you you can get the player from a Touched event:
ball.Touched:Connect(function(hit)
local character = hit:FindFirstAncestorOfClass("Model") --Tries to find character model
if(character) then
local player = game:GetService("Players"):GetPlayerFromCharacter(character) --Tries to find player
if(player) then
lastTouched = player
end
end
end)