I want to teleport the player in another area of the game(not different place), but I receive an error.
Error: attempt to index nil with ‘Character’
Script:
local plr = game.Players.LocalPlayer
game.ReplicatedStorage.Obby.Start.EasyStart.OnClientEvent:Connect(function(plr)
plr.Character.CFrame = script.Parent.Parent.Parent.Parts.Portal.CFrame
plr.Values.EasyObby.Value.Value = 180
local portal = game.Workspace.Obby.Easy.Portal.Portal
portal.BrickColor = BrickColor.new("Black")
portal.CanCollide = true
portal.Transparency = 0
while plr.Values.EasyObby.Value.Value do
wait(1)
if plr.Values.EasyObby.Value.Value > 0 then
plr.Values.EasyObby.Value.Value -= 1
end
portal.BrickColor = BrickColor.new("Lime green")
portal.CanCollide = true
portal.Transparency = 0.5
end
end)
If I make this work, I will be able to make the end teleport! Thanks for helping!
game.ReplicatedStorage.Obby.Start.EasyStart.OnClientEvent:Connect(function(plr)
while true do wait()
if plr.Character then break end
end
plr.Character.CFrame = script.Parent.Parent.Parent.Parts.Portal.CFrame
plr.Values.EasyObby.Value.Value = 180
local portal = game.Workspace.Obby.Easy.Portal.Portal
portal.BrickColor = BrickColor.new("Black")
portal.CanCollide = true
portal.Transparency = 0
while plr.Values.EasyObby.Value.Value do
wait(1)
if plr.Values.EasyObby.Value.Value > 0 then
plr.Values.EasyObby.Value.Value -= 1
end
portal.BrickColor = BrickColor.new("Lime green")
portal.CanCollide = true
portal.Transparency = 0.5
end
end)
Update: I tried to do everything I could do but I’m trapped in the “attempt to index nil” problem. I got “attempt to index nil with values” and then “waitForChild”.
My script: (no problem here)
script.Parent.Touched:Connect(function(hit)
if hit then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr then
local cooldown = plr.Values.EasyObby
local humanoidrootpart = plr.Character:FindFirstChild("HumanoidRootPart")
if humanoidrootpart then
if cooldown.Value == 0 then
game.ReplicatedStorage.Obby.Start.EasyStart:FireClient(plr)
plr.Character.CFrame = script.Parent.Parent.Parent.Parts.Portal.CFrame
print("Teleporting...")
end
end
end
end
end)
My local script in starterPlayerScripts:
local plr = game:GetService("Players").LocalPlayer
-- Easy Mode
game.ReplicatedStorage.Obby.Start.EasyStart.OnClientEvent:Connect(function(plr)
plr:WaitForChild("Values").EasyObby.Value = 180
local portal = game.Workspace.Obby.Easy.Portal.Portal
portal.BrickColor = BrickColor.new("Black")
portal.CanCollide = true
portal.Transparency = 0
while plr.Values.EasyObby.Value do
wait(1)
if plr.Values.EasyObby.Value > 0 then
plr.Values.EasyObby.Value -= 1
end
portal.BrickColor = BrickColor.new("Lime green")
portal.CanCollide = true
portal.Transparency = 0.5
end
end)
local plr = game.Players.LocalPlayer
game.ReplicatedStorage.Obby.Start.EasyStart.OnClientEvent:Connect(function(plr)
local character = plr.Character or plr.CharacterAdded:Wait()
character.HumanoidRootPart.CFrame = script.Parent.Parent.Parent.Parts.Portal.CFrame
plr.Values.EasyObby.Value.Value = 180
local portal = game.Workspace.Obby.Easy.Portal.Portal
portal.BrickColor = BrickColor.new("Black")
portal.CanCollide = true
portal.Transparency = 0
while plr.Values.EasyObby.Value.Value do
wait(1)
if plr.Values.EasyObby.Value.Value > 0 then
plr.Values.EasyObby.Value.Value -= 1
end
portal.BrickColor = BrickColor.new("Lime green")
portal.CanCollide = true
portal.Transparency = 0.5
end
end)
If this doesn’t work, remove “plr” in the function().
The error is right there… Remove plr from game.ReplicatedStorage.Obby.Start.EasyStart.OnClientEvent:Connect(function(plr) or either change local plr to local player.
local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
game.ReplicatedStorage.Obby.Start.EasyStart.OnClientEvent:Connect(function()
if Character then Character.CFrame = script.Parent.Parent.Parent.Parts.Portal.CFrame end
Player.Values.EasyObby.Value.Value = 180
local portal = game.Workspace.Obby.Easy.Portal.Portal
portal.BrickColor = BrickColor.new("Black")
portal.CanCollide = true
portal.Transparency = 0
while Player.Values.EasyObby.Value.Value do
task.wait(1)
if Player.Values.EasyObby.Value.Value > 0 then
Player.Values.EasyObby.Value.Value -= 1
end
portal.BrickColor = BrickColor.new("Lime green")
portal.CanCollide = true
portal.Transparency = 0.5
end
end)
You should not use a LocalScript to teleport a player’s character positionally because then it won’t replicate to other clients.
You should use a Server script.
Instead, you should just teleport a player’s character model using :PivotTo( the part’s cframe here ) whenever the player starts an obby whether through touching the part or joining the game.