I’m back with a new question developers,
I’m trying to right the script to check if the player press “E” to hatch the egg but when I try to check if the magnitude (distance between the player and the part (with BillBoard wich shows the pets rate and pets images) .
When I put my local script inside “StartPlayerScript” it works:
UserInputService = game:GetService(“UserInputService”)
UserInputService.InputBegan:Connect(function(input,gameProccesedEvent)
if input.KeyCode == Enum.KeyCode.E then
print(“E has been pressed”)
end
end)
But when I add the magnitude it doesn’t work, my script is:
UserInputService = game:GetService(“UserInputService”)
local player = game.Players.LocalPlayer
local humRootPart = player:FindFirstChild(“HumanoidRootPart”)
local part = workspace.CheckOutput
local magnitude = (humRootPart.Position - part.Position).Magnitude
UserInputService.InputBegan:Connect(function(input,gameProccesedEvent)
if input.KeyCode == Enum.KeyCode.E then
print(magnitude)
end
end)
The errormessage is : [12:24:19.370 - Players.Padrox_x.PlayerScripts.LocalScript:5: attempt to index nil with ‘Position’]
Does anyone know where is the problem?
Padrox
On your code you are trying to reference the HumanoidRootPart from the Player itself. However what you need to do is reference the HumanoidRootPart from the character. How can that be done? Just like this:
local player = game.Players.LocalPlayer;
local char = Player.Character or Player.CharacterAdded:Wait();
local HRP = char:WaitForChild("HumanoidRootPart");
player.CharacterAdded:Connect(function()
HRP = player.Character:FindFirstChild("HumanoidRootPart");
end)
local function checkMagnitude(p0, p1, distance)
-- Check is p0 is in range of distance from p1;
return (p0.Position - p1.Position).Magnitude <= distance;
end
-- Example of a use
local eggPart = workspace.EggPlatform;
if (not checkMagnitude(HumanoidRootPart, eggPart, 8)) then
-- player is too far from the eggPlatform;
return; -- terminate the rest of the script
end
-- Now if the player is in the range of the platform this code will run
-- Place your billboard UI etc.
local pets = {
[1] = {"PetName";Chance to get it(no floats)};
[2] = {"PetName";Chance to get it(no floats)};
--Repeat for every pet. Make sure it's in order (1 is highest chance...)
}
local event = game:GetService("ReplicatedStorage"):WaitForChild("EggHatch")
local function openEgg(player)
local totalWeight = 0
for i,v in pairs(pets) do
totalWeight = totalWeight + v[2]
end
local Chosen = math.random(1,totalWeight)
local pet = nil
for i,v in ipairs(pets) do
if Chosen >= v[2] then
pet = v[1]
break
end
end
event:FireServer(pet)
end
Storing the pets(Server):
local petStore = game:GetService("DataStoreService"):GetDataStore("Pets")
local petEvent = Instance.new("RemoteEvent")
petEvent.Name = "EggHatch"
petEvent.Parent = game:GetService("ReplicatedStorage")
game.Players.PlayerAdded:Connect(function(player)
local data pcall(function()
data = petStore:GetAsync(player.userId)
end)
if data == nil then
petStore:UpdateAsync(player.userId,function()
return {["Equipped"] = {};["Stored"] = {}}
end)
end
end)
petEvent.OnServerEvent:Connect(function(player,pet)
local pets = petStore:GetAsync(player.userId)
petStore:UpdateAsync(player.userId,function()
table.insert(pets,#pets+1,pet)
return pets
end)
end)
That’s good however you don’t want to trust the client as they could just spam the RemoteEvent with a rare / op pet and constantly get it. You should run the lottery function on the server, and then tell the client what pet they hatched.
Okay guys,
Thank you for all your help and tips but i’m a beginer and i’m not able to understand everything.
It looks like so hard for me.
I’m trying to adapt everything into my game but I’m absolutly not sure about what I’m doing.
What do you thing is the first step to do?
The pet hatching system or the Pet storage? Making all my pets first? Improve my pet following script?
Sorry I’m very confused in front of your scripts x’)
First thing first,
I need to make my pets follow me, no I want to say, I need to make my pet follow me (PERFECTLY x’) ).
Does anyone has a script to do that?
After, I want to know where should I put the pets after the egg hatching.
And third thing third, I need to save them.
If i’m right I have to create a table wich is saved in a DataStore called “Pets” or something like this.
After this I make a save when the player leave and i make a loading data when they join
True. This was taken from a script I made for a sim game I quit because it was sketchy so I didn’t really care if the client could spam rarest pet or not.
After a player hatches an egg the pet is inserted into the players data.
With that being said let’s say my data looked like this;
local DefaultData = {
Coins = 0;
Diamonds = 0;
Pets = {}; -- all the pets a player owns
};
Ok, so now we would insert the pet into the players data like this:
local playerData = DataStore2("PlayerData", player):Get(DefaultData);
local pet = {
uid = game:GetService("HttpService"):GenerateGUID(false);
name = petName;
s = petStrength;
e = false; -- equipped
xp = 0; -- exp
}
-- Insert pet into players datastore after egg hatch
table.insert(playerData.Pets, pet);
DataStore2("PlayerData", player):Set(playerData);
return pet, playerData;
For a pet following system I would check YouTube, as they can be simple like the tutorials or complex which would include RayCasting, Tweening, etc.
I already have a dataStore for coins and other leaderstats statistics.
Shoud I remove it to replace it with the dataStore 2 to save coins and other things + pets?
Oh my god…
I’m soooooo lost…
I’m so so so sorry to make you loose your time for usual things like this…
I want to learn but I’m so lost even if I watched tons & tons of videos.
Can I ask you to join one of my test place in the coming days to show me some of your scripts because I don’t know how to change things, where to put scripts and every details like this.
I’m sorry and i’m able to understand if you can’t afford yourself to join me.
See ya