I am quit sure the problem is coming from me teleporting the player. I’ve saw other post of people who have the same error but I’m not understanding how they were asked to fix it and their problems were all about changing a variable inside of a function that detects when the variable changes.
function onTouch(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player ~= nil and player.Level.Value >= 20 then
player.Character.Humanoid.Torso.CFrame = CFrame.new(496, -393.9, -364)
elseif player ~= nil then
print(player.Name.." isn't at least lvl 20!")
end
end
script.Parent.Touched:connect(onTouch)
function onTouch(hit)
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player ~= nil and player.Level.Value >= 20 then
player.Character.HumanoidRootPart.CFrame = CFrame.new(496, -393.9, -364)
elseif player ~= nil then
print(player.Name.." isn't at least lvl 20!")
end
end
script.Parent.Touched:connect(onTouch)
@JBhappy2 has the solution, but I wanted to clean the script up a bit.
Mainly on the part that you don’t need to do object ~= nil or object == nil. All you need to do is if object then or if not object then.
script.Parent.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild('Humanoid') then
local player = game:GetService('Players'):FindFirstChild(hit.Parent.Name)
if player then
if player.Level.Value >= 20 then --Note that this may be player.leaderstats.Level.Value
player.Character.HumanoidRootPart.CFrame = CFrame.new(496,-393.9,-364)
else
print(player.Name.." isn't at least lvl 20!")
end
end
end
end)
Thank you! I’m currently updating all the code to an RPG I used to play when I was a kid, but I’ve only been programming for a week or two so I’m still learning what practices are bad and what things I should be fixing. I’m also still learning to read the code and fully make use of my knowledge while doing so. I totally knew that that should have been referencing the HumanoidRootPart instead but completely overlooked it! You guys have been a great help!