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

Why is the touched:connect function getting called with nil?

Asked by 5 years ago
Edited 5 years ago

Here's my module

I get a "rightFoot" "here1" and "nil" when the touch happens. The first function is called properly from another checkpoint script.

Here's the call:

local playerDataManager = require(workspace.PlayerDataManager)

local deadBoard = script.Parent
deadBoard.Touched:connect(function(hit)
    print (hit)
    playerDataManager.moveToLastCheckpoint(hit)
end) 

And here's the module:

local PlayerDataManager = {}

local DataStoreService = game:GetService("DataStoreService")
local playersDataStore = DataStoreService:GetDataStore("PlayerData")


local sessionData = Instance.new("Model")
sessionData.Name = "SessionData"

local players = game:GetService("Players")

function PlayerDataManager:recordCheckpoint(hit,checkpointPart)
    print(hit)
    if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
        local player = players:GetPlayerFromCharacter(hit.Parent)

        local playerData = sessionData:FindFirstChild(tostring(player.userId))
        --local checkpoint = checkpointData:FindFirstChild(tostring(player.userId))

        if not playerData then
            playerData = Instance.new("Model", sessionData)
            playerData.Name = tostring(player.userId)

            player.CharacterAdded:connect(function(character)
                wait()
                character:WaitForChild("HumanoidRootPart").CFrame =  game.ServerStorage.CheckpointData[tostring(player.userId)].Value.CFrame * CFrame.Angles(0, 0, math.rad(-90)) + Vector3.new(0, 10, 0)
            end)
        end

        local lastCheckpoint = playerData:FindFirstChild("LastCheckpoint")
        if not lastCheckpoint then
            lastCheckpoint = Instance.new("ObjectValue", playerData)
            lastCheckpoint.Name = "LastCheckpoint"
        end

        lastCheckpoint.Value = checkpointPart
        print(sessionData:FindFirstChild(tostring(player.userId)))
    end
end

function PlayerDataManager:moveToLastCheckpoint(hit)
    print("here1")
    print(hit)
    if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
        local player = players:GetPlayerFromCharacter(hit.Parent)
        print("here2")
        local playerData = sessionData:FindFirstChild(tostring(player.userId))
        if playerData then
            print(playerData)
            local lastCheckpoint = playerData:FindFirstChild("LastCheckpoint")
            if lastCheckpoint then
                print(lastCheckpoint)
                player.character:WaitForChild("HumanoidRootPart").CFrame =  lastCheckpoint.Value.CFrame * CFrame.Angles(0, 0, math.rad(-90)) + Vector3.new(0, 10, 0)
            end
        end

    end
end

return PlayerDataManager

1 answer

Log in to vote
0
Answered by 5 years ago

Nevermind. &*$!ing colons. Seriously, why colons?

Need to change line 6 from

playerDataManager.moveToLastCheckpoint(hit)

to

playerDataManager:moveToLastCheckpoint(hit)
0
Colons are used for methods in Lua. That's why. DeceptiveCaster 3761 — 5y
Ad

Answer this question