I'm trying to make a system where you spawn as a rat in a game I'm making, but the system isn't working despite me having looked up multiple tutorials on the subject. So how do I fix this? Here is the code in "PlayerHandler":
local ss = game:GetService("ServerStorage") local bs = game:GetService("BadgeService") local rmodule = require(ss.RatModule) local umodule = require(ss.UserModule) repeat wait() until rmodule and umodule game.Players.PlayerAdded:Connect(function(player) umodule.AwardBadge(player, *WELCOMEBADGEID*) -- badge hidden for obvious reasons player.CharacterAdded:Wait() local rat = umodule.GetRatFromName("Test Rat") -- RAT PROCESS: -- morphing local model = ss.RatModels:FindFirstChild(rat.Model):Clone() model.Name = player.Name model:SetPrimaryPartCFrame(player.Character:GetPrimaryPartCFrame()) for _, v in pairs(model:GetDescendants()) do if v:IsA("BasePart") or v:IsA("MeshPart") then v.Anchored = false end end -- adding scripts local scripts = ss.RatScripts:FindFirstChild(rat.ScriptsName) if scripts then for _, _script in pairs(scripts:GetChildren()) do _script:Clone().Parent = model end end -- finishing morph player.Character:Destroy() player.Character = model model.Parent = workspace end)
Here is the code for the "UserModule":
local dservice = game:GetService("DataStoreService") local ds = dservice:GetDataStore(*DATASTORENAME*) -- name not shown for privacy purposes local module = {} module.Defaults = { Defaults = {"Rats", "SelectedRat", "Money"}, -- In Order RatDefaults = { { ["Name"] = "Rat", ["Model"] = "Rat" } }, RatDefault = "Rat", MoneyDefault = 0 } function module.GetRatFromName(name) local ss = script.Parent local rmodule = require(ss.RatModule) repeat wait() until rmodule for _, rat in pairs(rmodule.Rats) do if rat.Name == name then return rat end end return nil end function module.GetUserData(player) local user = {} local rats = ds:GetAsync("RatData_Rats_" .. player.UserId) if rats then user.Rats = rats -- print("Player rat data exists!") end local selected = ds:GetAsync("RatData_SelectedRat_" .. player.UserId) if selected then user.SelectedRat = selected -- print("Player selected data exists!") end local money = ds:GetAsync("RatData_Money_" .. player.UserId) if money then user.Money = money -- print("Player money data exists!") end return user end function module.SetDefaults(player) local user = module.GetUserData(player) for i = 1, #module.Defaults.Defaults do user[module.Defaults.Defaults[i]] = module.Defaults[i + 1] ds:SetAsync("RatData_" .. module.Defaults.Defaults[i] .. "_" .. player.UserId, module.Defaults[i + 1]) end return user end function module.AwardRat(data, player) local user = module.GetUserData(player) for _, rat in pairs(user.Rats) do if rat.Name == data.Name then return false, player.Name .. " already has rat: " .. data.Name end end table.insert(user.Rats, data) ds:SetAsync("RatData_Rats_" .. player.UserId, user.Rats) return true, player.Name .. "got rat: " .. data.Name end function module.AwardBadge(player, id) local bs = game:GetService("BadgeService") if not bs:UserHasBadgeAsync(player.UserId, id) then local success, message = pcall(function() bs:AwardBadge(player.UserId, id) end) return success, message end return false, "User already has badge" end function module.CheckRat(data, player) local user = module.GetUserData(player) for _, rat in pairs(user.Rats) do if rat.Name == data.Name then return true end end return false end return module
And lastly, here's the "RatModule":
local module = { ["Rats"] = { { ["Name"] = "Rat", ["Model"] = "Rat", ["ScriptsName"] = "Rat", ["Description"] = "the default rat", ["UnlockType"] = "Default" }, { ["Name"] = "Floppa", ["Model"] = "Floppa", ["ScriptsName"] = "Floppa", ["Description"] = "play raf(2)", ["UnlockType"] = "Purchasable" }, { ["Name"] = "Test Rat", ["Model"] = "TestRat", ["ScriptsName"] = "Rat", ["Description"] = *DESCRIPTION*, -- description not shown for surprise reasons ["UnlockType"] = "Staff" } }, ["RatType"] = { ["Default"] = 0, ["Unlockable"] = 1, ["Purchasable"] = 2, ["Staff"] = 3, } } return module