I have these scripts here, and i want to activate them only by touching a part, so only if i touch a certain part the position saves. first script:
game.Players.PlayerAdded:Connect(function(player)
local val = player:WaitForChild("Position")
player.CharacterAdded:connect(function(char)
while true do
wait(1)
val.Value = char:WaitForChild("HumanoidRootPart").Position
end
end)
end)
Second script:
local ds = game:GetService("DataStoreService")
local ds1 = ds:GetDataStore("PositionSAvingSystem")
game.Players.PlayerAdded:Connect(function(player)
local val = Instance.new("Vector3Value",player)
val.Name = "Position"
local userdata
pcall (function()
userdata = ds1:GetAsync(player.UserId)
end)
if userdata then
game.Workspace:WaitForChild(player.name):WaitForChild("HumanoidRootPart").CFrame = CFrame.new(userdata.x, userdata.y, userdata.z)
else
ds1:SetAsync(player.UserId, {
x= 0,
y= 3,
z= 0
})
end
end)
game.Players.PlayerRemoving:Connect(function(player)
ds1:SetAsync(player.UserId, {
x = player.Position.Value.x,
y = player.Position.Value.y,
z = player.Position.Value.z
})
end)

1 comment
I don't know much about DataStores but you have a line of codes between
PlayerAddedandCharacterAdded, because of that, the codes after that line won't work sinceCharacterAddedfires immediately once a character is added to theWorkspace, try to move that code afterCharacterAddedinstead.