This is longer than I expected, so here's the uncopylocked game with a free BASIC follow pet script and a notification module.
Alright, while I don't know your exact problem because you haven't provided any output information; I can try and deduce what is wrong and explain how you can move forward and secure your code in the process to help save you from major headaches regarding the client-server model in ROBLOX.
Objects that you put in ReplicatedFirst run, well, absolutely first. You don't store storage objects there. As the contents of ReplicatedFirst replicate to the client before anything else in the game. it is ideal for creating loading GUIs or tutorials. In your case, a pet model does not meet this criteria, so it shouldn't be there. ReplicatedStorage is where you can store references to objects that need to be "Replicated" to the client. Both the client and server can see these objects. In your case however, unless you need to access this model from the client to show in a Viewport for instance, your pet models can be placed in ServerStorage, away from prying eyes. This also helps with lag, as anything placed in ReplicatedStorage will be replicated to the player.
*NEVER TRUST THE CLIENT to do anything regarding sanity checks that are going to give them leverage. The client is a liar, anything they say can and will be used against you. *
Think of me as bank teller and you as the customer. If you told me you had 1million dollars to withdraw, would I just give it to you or would I look you up in the database first? I'm not going to trust you to tell me what you have; I'm going to verify your balance and I'm going to verify you are who you say you are.
When you are asking the player if they enough rebirths to unlock the pet, all a player has to do is change their leaderstats, which can be done very easily. Instead, what you can and should do is keep this check, but then also fire a RemoteEvent to the server that checks again and handles the rest on the server.
LocalScript in your GUI
local LocalPlayer = game.Players.LocalPlayer
local leaderstats = LocalPlayer:WaitForChild("leaderstats")
local rebirths = leaderstats:WaitForChild('rebirths')
local button = script.Parent
local PetGiverEvent = game:GetService("ReplicatedStorage"):WaitForChild('PetGiver')
local NotificationEvent = game:GetService("ReplicatedStorage"):WaitForChild('Notification')
NotificationEvent.OnClientEvent:Connect(function(msg)
print ( msg )
end)
button.MouseButton1Down:Connect(function()
if rebirths.Value >= 100 then
PetGiverEvent:FireServer('Dragon')
end
end)
Script in ServerScriptService
local PetStorage = game:GetService('ServerStorage').Pets
local RemoteEvent = game:GetService("ReplicatedStorage").PetGiver
local NotificationModule = require ( script.Notification )
local Pets = {
['Dragon'] = { Pet = PetStorage:WaitForChild("Dragon"), Cost = 100 }
}
local PlayerPets = {}
local function Assign(Player, newPet)
if PlayerPets[Player.Name] then
PlayerPets[Player.Name]:Destroy()
end
local Pet = newPet and Pets[newPet].Pet:Clone() or Pets[PlayerPets[Player.Name].Name].Pet:Clone()
Pet.Parent = Player.Character
PlayerPets[Player.Name] = Pet
repeat wait() until Player.Character
Pet.PrimaryPart:SetNetworkOwner(Player)
end
RemoteEvent.OnServerEvent:Connect(function(PlayerWhoFired, PetName)
assert(PetName, 'Provide a PetName')
assert(typeof(PetName) == 'string', 'PetName has to be a string!')
local MatchingKey = Pets[PetName]
if Pets[PetName] then
local leaderstats = PlayerWhoFired:FindFirstChild("leaderstats")
local rebirths = leaderstats and leaderstats:FindFirstChild("rebirths")
if rebirths then
if rebirths.Value >= MatchingKey.Cost then
Assign(PlayerWhoFired, PetName)
else
NotificationModule.Message(PlayerWhoFired, "You don't have enough")
end
end
else
NotificationModule.Message(PlayerWhoFired, 'That pet does not exist')
end
end)
game.Players.PlayerAdded:Connect(function(Player)
script.leaderstats:Clone().Parent = Player
Player.CharacterAdded:Connect(function(character)
if PlayerPets[Player.Name] then
Assign(Player)
end
end)
end)
game.Players.PlayerRemoving:Connect(function(Player)
PlayerPets[Player.Name] = nil
end)
1 comment
do you have more than one 'Part' in replicatedFirst?