I am making an obby. As usual I have special parts like Kill Bricks, Conveyors, Trampolines and Spawnpoints.
Today I have issues with spawnpoints. The issue is not detecting the spawnpoint.
You will already read the comments in below, script says the ObjectValue I created using Instance.new() in another script doesn’t exist.
BasisModule: ModuleScript - Handling Spawnpoint
local BasisTypes = {
["Spawnpoint"] = function(Part: BasePart)
Part.Touched:Connect(function(OtherPart)
if not table.find(BodyPartsR6, OtherPart.Name) then return end
local Humanoid = OtherPart.Parent:FindFirstChild("Humanoid")
if Humanoid and Humanoid.Health > 0 then
print("Touched spawnpoint and humanoid found.")
local Player = Players:GetPlayerFromCharacter(Humanoid.Parent)
if ServerStorage.Spawnpoints:FindFirstChild(Player.UserId) then
ServerStorage.Spawnpoints[Player.UserId].Value = Part
--> Value is never set because there is this if statement.
--> As you can see, above line says that Player.UserId doesn't exist
--> in the ServerStorage.Spawnpoints
end
end
end)
end
}
function module.HandleBasisInstance(BasisInstance: Instance)
if BasisInstance:IsA("BasePart") then
local Attribute = BasisInstance:GetAttribute("BasisType")
if Attribute and BasisTypes[Attribute] then
BasisTypes[Attribute](BasisInstance)
end
end
end
MainScript: Script - Calling HandleBasisInstance()
_Workspace.DescendantAdded:Connect(function(Descendant: Instance)
if Descendant:IsA("BasePart") then
BasisModule.HandleBasisInstance(Descendant)
end
end)
PlayerScript: Script - Teleport player to spawnpoint
Players.PlayerAdded:Connect(function(Player)
local Leaderstats = Instance.new("Folder", Player)
Leaderstats.Name = "leaderstats"
--Initialize other leaderstats instances
--If available get data from DataStore
local Spawnpoint = Instance.new("ObjectValue", ServerStorage.Spawnpoints)
Spawnpoint.Name = Player.UserId
--> As you can see above Player.UserId is created in ServerStorage.Spawnpoints
--Update leaderstats when spawnpoint changed
--CharacterAdded events
Player.CharacterAppearanceLoaded:Connect(function(Character)
if Spawnpoint.Value ~= nil then --> Always returns nil because value is never set
Character:PivotTo(Spawnpoint.Value.CFrame + Vector3.new(0, 4, 0))
end
end)
end)