Hello! I have a question, how do I get the character in this server script. If I put it in a local script nothing happens, I want to teleport the player if the box touches the part but is there any way to fix this.
Any help is appreciated!
Im still a beginner in scripting.
Here is the code:
script.Parent.Touched:Connect(function(hit)
local box = hit.Parent:FindFirstChild("Box")
if box then
local char = game.Players.LocalPlayer.Character
char:PivotTo(game.Workspace["lvl 2"].lvl2teleport:GetPivot())
end
end)
Hello! As I saw there are a few mistakes in your code, this is a code that should work for your task.
script.Parent.Touched:Connect(function(hit)
local box = hit.Parent:FindFirstChild("Box")
if box then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
local char = player.Character
if char then
char:MoveTo(game.Workspace["lvl 2"].lvl2teleport.Position)
end
end
end
end)```
What you can do is simply adding in a :GetPlayerFromCharacter() event. This is an example script.
local Part = script.Parent
local DB = false
Part.Touched:Connect(function(OnHit)
local Player = game:GetService("Players"):GetPlayerFromCharacter(OnHit.Parent)
if Player and not DB then
-- Your code here.
end
end)