I am getting this error on my code and I really don’t know why. Please help, thanks!
The error:
The code:
task.wait(3)
--Services
local players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")
--Events
local SendGUI = RS:WaitForChild("Racing"):WaitForChild("SendGUI")
local Bindable = RS:WaitForChild("Racing"):WaitForChild("Bindable")
--Items
local Racetrack1 = script.Parent
local PlayersFolder = Racetrack1.Players
local FinishLine = Racetrack1.FinishLine
local Participants = Racetrack1.Participants
local Ongoing = Racetrack1.Ongoing
local debounce = false
local place = 0
FinishLine.Touched:Connect(function(hit)
if debounce == false then
debounce = true
warn("I have received the touch")
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if Ongoing.Value == true then
warn("I've seen that the value is true")
if players:FindFirstChild(player.Name) and PlayersFolder:FindFirstChild(player.Name) then --This is where the error is located
warn("I have found player, so both of the things are found.")
place = Participants.Value - (Participants.Value - 1)
PlayersFolder:WaitForChild(player.Name).Value = place
local otherserver = 2
Bindable:Fire(otherserver, player)
warn("I've also fired the bindable event to the script in sss, my job is done here :D")
end
end
wait(0.1)
debounce = false
end
end)
Okay, the problem is you are not checking if the player does exist (before trying to index .Name in your checks). If the :GetPlayerFromCharacter() function cannot get a player (e.g. the hit.Parent is not a player model) it will return nil and therefore you will get the error. The corrected version of your script that checks if there is a player that has been returned is:
task.wait(3)
--Services
local players = game:GetService("Players")
local RS = game:GetService("ReplicatedStorage")
--Events
local SendGUI = RS:WaitForChild("Racing"):WaitForChild("SendGUI")
local Bindable = RS:WaitForChild("Racing"):WaitForChild("Bindable")
--Items
local Racetrack1 = script.Parent
local PlayersFolder = Racetrack1.Players
local FinishLine = Racetrack1.FinishLine
local Participants = Racetrack1.Participants
local Ongoing = Racetrack1.Ongoing
local debounce = false
local place = 0
FinishLine.Touched:Connect(function(hit)
if debounce == false then
debounce = true
warn("I have received the touch")
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player and Ongoing.Value == true then --Check if the player exists on this line.
warn("I've seen that the value is true")
if players:FindFirstChild(player.Name) and PlayersFolder:FindFirstChild(player.Name) then --This is where the error is located
warn("I have found player, so both of the things are found.")
place = Participants.Value - (Participants.Value - 1)
PlayersFolder:WaitForChild(player.Name).Value = place
local otherserver = 2
Bindable:Fire(otherserver, player)
warn("I've also fired the bindable event to the script in sss, my job is done here :D")
end
end
wait(0.1)
debounce = false
end
end)