This isn’t a very good Touched
event, and the script @Mikixxx0 is providing isn’t helpful either
Your Issue is that you are Attempting to get a Property what Doesn’t exist within the Object you are looking through, This is because you have to check for the Object to ensure that it is what youre looking for, in this case, we will look for the Humanoid
, A Component to our Characters that will help use Identify them, this is your Touched
event:
endPad.Touched:Connect(function(otherPart)
Touched
doesnt return a Player, It returns a BasePart
if you read what It does:
Touched:Connect(func: (otherPart: BasePart) -> ()): RBXScriptConnection
- The
func
part means it requires a function
- The
(otherPart: BasePart)
means that the first argument is going to be a Part, not a Player
- The
: RBXScriptConnection
is what’s being returned, which in this case, its the Signal used for the Event.
This will Help us understand what Touched
does as now, we know that it returns a BasePart
BasePart
is basically the class Part
, MeshPart
, WedgePart
, etc can refer to, so any Part that touches the Object will be referred here.
In order to check if the Object we are getting is the Player’s Character, we have to do a multitude of things, which is to find the Humanoid
of the Character, the reason we do this is so we can reference the Player.
But “we can do this without Having Errors!”, we use FindFirstChild
, with FindFirstChild
we can check if an Item exists within an Instance
, if its not Found, it will return nil
, otherwise it will return the Object
, we can then check if the Item actually Exists using a simple if statement, here is an Example:
(Note that this Code block isnt Part of the Code, Just as an Example)
local Child = Instance:FindFirstChild("Child") -- Gets the Object named "Child"
if Child then -- if "Child" was found
-- code here if the object was found
else
warn"Child not Found!"
end
So now that we know how to do this, we can Check for the Humanoid
:
local Hum = otherPart.Parent:FindFirstChildOfClass("Humanoid") -- Gets the First Humanoid in the Character
if Hum then -- If the Humanoid Exists
-- code in a sec
end
We check the Parent of the Part because when something hits the endPart
, it will return the Part, not the Model, with this check, we will be doing this:
OtherPart.Parent = Model
Model:FindFirstChild("Humanoid") (Humanoid or nil)
We then check if the Object is a Player or not by using GetPlayerFromCharacter
as stated above.
What this will do is check if the Instance
is the Character
of a Player
, if that is true, it will return a Player
, otherwise, it will return nil
, so like with the other Example, we will check if there is a Player:
local Hum = otherPart.Parent:FindFirstChildOfClass("Humanoid") -- Gets the First Humanoid in the Character
local Player = game.Players:GetPlayerFromCharacter(otherPart.Parent) -- checks the Model if its a Character for the Player
if Hum then -- If the Humanoid Exists
if Player then -- if the Player was found
-- Place your code here for the Badge
end
end
So In total, you need to check for this stuff to make sure you are getting the Stuff you are looking for, this should be the full code:
endPad.Touched:Connect(function(otherPart) -- Event
local Hum = otherPart.Parent:FindFirstChildOfClass("Humanoid") -- Gets the First Humanoid in the Character
local Player = game.Players:GetPlayerFromCharacter(otherPart.Parent) -- checks the Model if its a Character for the Player
if Hum then -- If the Humanoid Exists
if Player then -- if the Player was found
if not badgeservice:UserOwnsBadgeAsync(Player.UserId,BadgeID) then -- if User does not have the Badge
badgeservice:AwardBadge(Player.UserId,BadgeID)
end
end
end
end)