Advertisement
PheonixStudio

Pet Script (Server Script)

May 31st, 2020
1,175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.72 KB | None | 0 0
  1. --// Variables
  2. local RunService = game:GetService("RunService").Stepped --| Since Scripts can't access RenderStepped or Heartbeat we can simply put Stepped.
  3.  
  4. local Event = game.ReplicatedStorage.Equip
  5. local PetFolder = game.Workspace:WaitForChild("PetsFolder")
  6.  
  7.  
  8. --// Functions
  9. game.Players.PlayerRemoving:Connect(function(plr)
  10.     --| We don't want users to have pets just hanging around after the user has left right?
  11.     --| This checks if the user has a pet still instanced in the Folder and deletes it.
  12.    
  13.     if PetFolder:FindFirstChild(plr.Name) then
  14.         PetFolder[plr.Name]:Destroy()
  15.     end
  16. end)
  17.  
  18. Event.OnServerEvent:Connect(function(plr, _Pet)
  19.     --| _Pet is the pet we want to clone. Sent from the client, Take note try and not to trust the client.
  20.     --| This event isn't practical however you can add a "Legitify" if statement to check if the user does infact own the pet sent from the client.
  21.     --| I.E leaderstats. Check if the _Pet exists and then check if the user owns it.
  22.    
  23.     if not PetFolder:FindFirstChild(plr.Name) then --| If the user has a pet already equipped.
  24.         local hmr = plr.Character:WaitForChild("HumanoidRootPart")
  25.        
  26.         local Pet = _Pet:Clone()
  27.         Pet.Parent = PetFolder
  28.         Pet.Name = plr.Name
  29.        
  30.         local P, G --| P = Body Position, G = BodyGyro.
  31.        
  32.         if Pet:IsA("BasePart") then --| Makes this compatible with Models or just parts!
  33.             P = Pet.BodyPosition
  34.             G = Pet.BodyGyro
  35.         elseif Pet:IsA("Model") then
  36.             P = Pet.PrimaryPart.BodyPosition
  37.             G = Pet.PrimaryPart.BodyGyro
  38.         end
  39.        
  40.         RunService:Connect(function()
  41.             P.Position = hmr.Position + 3 * hmr.CFrame.RightVector + 1.5 * hmr.CFrame.UpVector - 3 * hmr.CFrame.lookVector
  42.             G.CFrame = hmr.CFrame
  43.         end)   
  44.     else
  45.         PetFolder[plr.Name]:Destroy()
  46.     end
  47. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement