Hey there!
I’m a bit stuck on my script right now .
I’m trying to make an egg hatching system on my Roblox Game.
I successfully achieve the script to check the magnitude (distance between the egg and the player) + the UserInputService (Press E to hatch the egg). I put the script into “PlayerStarterScript”.
I replace the ClickDetector by the UserInput, but I have some problems.
The first is my script is located into the StarterPlayerGui(LocalScript) and his script is inside the clickdetector(script).
I tried to adapt his script into my InputService but I have an errormessage wich I don’t understand.
This how my script looks like now:
UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer;
local char = player.Character or player.CharacterAdded:Wait();
local HRP = char:WaitForChild("HumanoidRootPart");
local ReplicatedStorage = game:GetService("ReplicatedFirst")
local DataModule = require(game:GetService("ReplicatedStorage"):WaitForChild("DataModule"))
local Pets = ReplicatedStorage:WaitForChild("Pets")
local eggPart = workspace["Common Egg"].eggBasePart
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
UserInputService.InputBegan:Connect(function(input,gameProccessedEvent)
if input.KeyCode == Enum.KeyCode.E then
if (not checkMagnitude(HRP, eggPart, 20)) then
-- Player is too far from the eggPart
return; -- Terminate the rest of the script
end
if player.leaderstats.Coins.Value >= DataModule.Common.EggCost then
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - DataModule.Common.EggCost
local function ChoosePet(player)
local PlayerGui = player:WaitForChild("PlayerGui")
local PetGui = PlayerGui.PetGui
local EggIcon = PetGui.EggIcon
local PetImages = PetGui.PetImages
local timer = tick()
EggIcon.Visible = true
while tick() - timer < DataModule.Common.EggOpeningLength do
wait(.1)
EggIcon.Rotation = math.random(-9,9)
end
local petNumber = math.random(1,50)
local petFound = false
local pet = nil
while petFound == false do
for i, v in pairs(DataModule.Common.Pets) do
if math.random(1,v) == petNumber then
petFound = true
pet = i
end
end
end
EggIcon.Visible = false
local PetImage = PetImages:FindFirstChild(pet)
PetImage.Visible = true
wait(2)
PetImage.Visible = false
return pet
end
end
end
end)
I think the problem come from the localisation of my script.
I don’t have any idea to fix it.
So if I’m right, I have to define the ChoosePet function out my UserInput function?
So it should be something like this:
UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer;
local char = player.Character or player.CharacterAdded:Wait();
local HRP = char:WaitForChild("HumanoidRootPart");
local ReplicatedStorage = game:GetService("ReplicatedFirst")
local DataModule = require(game:GetService("ReplicatedStorage"):WaitForChild("DataModule"))
local Pets = ReplicatedStorage:WaitForChild("Pets")
local eggPart = workspace["Common Egg"].eggBasePart
local function ChoosePet(player)
local PlayerGui = player:WaitForChild("PlayerGui")
local PetGui = PlayerGui.PetGui
local EggIcon = PetGui.EggIcon
local PetImages = PetGui.PetImages
local timer = tick()
EggIcon.Visible = true
while tick() - timer < DataModule.Common.EggOpeningLength do
wait(.1)
EggIcon.Rotation = math.random(-9,9)
end
local petNumber = math.random(1,50)
local petFound = false
local pet = nil
while petFound == false do
for i, v in pairs(DataModule.Common.Pets) do
if math.random(1,v) == petNumber then
petFound = true
pet = i
end
end
end
EggIcon.Visible = false
local PetImage = PetImages:FindFirstChild(pet)
PetImage.Visible = true
wait(2)
PetImage.Visible = false
return pet
end
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
UserInputService.InputBegan:Connect(function(input,gameProccessedEvent)
if input.KeyCode == Enum.KeyCode.E then
if (not checkMagnitude(HRP, eggPart, 20)) then
-- Player is too far from the eggPart
return; -- Terminate the rest of the script
end
if player.leaderstats.Coins.Value >= DataModule.Common.EggCost then
player.leaderstats.Coins.Value = player.leaderstats.Coins.Value - DataModule.Common.EggCost
ChoosePet()
end
end
end)
In ChoosePet a required argument is Player, delete this required argument as its defined above:
local function ChoosePet()
local PlayerGui = player:WaitForChild("PlayerGui")
local PetGui = PlayerGui.PetGui
local EggIcon = PetGui.EggIcon
local PetImages = PetGui.PetImages
local timer = tick()
EggIcon.Visible = true
while tick() - timer < DataModule.Common.EggOpeningLength do
wait(.1)
EggIcon.Rotation = math.random(-9,9)
end
local petNumber = math.random(1,50)
local petFound = false
local pet = nil
while petFound == false do
for i, v in pairs(DataModule.Common.Pets) do
if math.random(1,v) == petNumber then
petFound = true
pet = i
end
end
end
EggIcon.Visible = false
local PetImage = PetImages:FindFirstChild(pet)
PetImage.Visible = true
wait(2)
PetImage.Visible = false
return pet
end
What you do with the pet is your decision, as the pet is returned I would most likely create a pet handling scenario with said pet and saving it. This is obviously all your choice now. And if you want to create more threads about making a pet system etc then feel free to, but obviously try it yourself first and see where you can get.
Can you become my Roblox Scripter Tutor?
I can understand if you wont for multiple reasons even because i’m probably a lost of time for you.
But I think I have so much more to learn from you as myself.
I’m unfortunately not doing tutoring right now, however I’m working on a community resource to help people with self tutoring etc. so I’d look out for that monstrosity when its done.
Its fine by the way, I do this at your pleasure as its fun and interesting - just obviously make sure to have a break once you’ve done something big like this so you don’t work yourself up