Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Help me activate my position save script?

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

0

I don't know much about DataStores but you have a line of codes between PlayerAdded and CharacterAdded, because of that, the codes after that line won't work since CharacterAdded fires immediately once a character is added to the Workspace, try to move that code after CharacterAdded instead.

1 answer

you would do that with a touch function like this

local player  = game.Players.LocalPlayer
local character = player.Character
local Humanoid = character:WaitForChild("Humanoid")

YourPart.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then --I edited this
	--Code Here
	end
end)

4 comments

0

it doesnt work

0

is there any errors in the output?

0

Oh wait I think I did the "if" line wrong, I changed it though. It is supposed to be "if hit.Parent:FindFirstChild("Humanoid") not "if hit.Parent == Humanoid"

0

Truth is, thats only to check if it's touched by a player, if you want it to enable while being touched as a part, then jut remove the if and normally put your code.

This discussion is preserved as it was. New answers, comments, and votes are closed.