Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Help me activate my position save script?

Asked by 3 years ago
Edited 3 years ago

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)
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. NotTheChara 191 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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)
0
it doesnt work Just_T0m 1 — 3y
0
is there any errors in the output? ShineBright2775 30 — 3y
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" ShineBright2775 30 — 3y
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. Frometa1998 35 — 3y
Ad

Answer this question