So I have a food eating system for my game, where it checks certain requirements before actually destroying itself and adding the stats for the player. The problem is, this waiting time can take up to 1-2 seconds, which leads to a noticeable delay when players try to eat the food. For example, a player could touch the food and be 10 studs away and suddenly see the food get destroyed. Is there any thing that is specifically doing this and is there a way I can speed this script up?
rpvefood.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if hit.Parent.Humanoid.Health > 0 and plr then
local data = GetData(plr)
if data then
if data.GameData.PVP.Value == false then
if data.GameData.Size.Value < 500 then
rpvefood:Destroy()
data.Totals.TotalFoodEaten.Value = data.Totals.TotalFoodEaten.Value + 1
data.Totals.FoodEaten.Value = data.Totals.FoodEaten.Value + 1
if data.Gamepasses.DoubleMass.Value == true then
data.GameData.Size.Value = data.GameData.Size.Value + 2
if data.GameData.Size.Value > 500 then
data.GameData.Size.Value = 500
end
else
data.GameData.Size.Value = data.GameData.Size.Value + 1
end
if data.Gamepasses.DoubleCoins.Value == true then
data.Totals.Coins.Value = data.Totals.Coins.Value + 2
game.ReplicatedStorage.Remotes.GotPopUp:FireClient(plr, 2, "Coins", Color3.fromRGB(255, 255, 0))
else
data.Totals.Coins.Value = data.Totals.Coins.Value + 1
game.ReplicatedStorage.Remotes.GotPopUp:FireClient(plr, 1, "Coins", Color3.fromRGB(255, 255, 0))
end
IncreaseXP(plr, 1)
SetSize(plr)
SpawnFood(1)
else
game.ReplicatedStorage.Remotes.ErrorMessage:FireClient(plr, "Max size for PVE reached! Enable PVP to grow bigger!")
end
elseif data.GameData.PVP.Value == true then
if data.GameData.Size.Value < 4000 then
rpvefood:Destroy()
data.Totals.TotalFoodEaten.Value = data.Totals.TotalFoodEaten.Value + 1
data.Totals.FoodEaten.Value = data.Totals.FoodEaten.Value + 1
if data.Gamepasses.DoubleMass.Value == true then
data.GameData.Size.Value = data.GameData.Size.Value + 2
if data.GameData.Size.Value >= 4000 then
data.GameData.Size.Value = 4000
game.ReplicatedStorage.Remotes.ErrorMessage:FireClient(plr, "Max mass reached!")
end
else
data.GameData.Size.Value = data.GameData.Size.Value + 1
end
if data.Gamepasses.DoubleCoins.Value == true then
data.Totals.Coins.Value = data.Totals.Coins.Value + 2
game.ReplicatedStorage.Remotes.GotPopUp:FireClient(plr, 2, "Coins", Color3.fromRGB(255, 255, 0))
else
data.Totals.Coins.Value = data.Totals.Coins.Value + 1
game.ReplicatedStorage.Remotes.GotPopUp:FireClient(plr, 1, "Coins", Color3.fromRGB(255, 255, 0))
end
IncreaseXP(plr, 1)
SetSize(plr)
SpawnFood(1)
end
end
end
end
end
end)