-
What do you want to achieve? Keep it simple and clear!
I’ve made this script that makes a part or meshpart that is added to a folder, follow the player… and if the part is called “Skull” also faces the player as it follows him. The script achieves its goal but I want to it to be less cpu consuming. -
What is the issue? Include screenshots / videos if possible!
The script is consuming too much CPU causing lag even if there is no part added or
the part is allready destroyed. Also the Script Performance window shows peaks of the script rate from 76/s to 150/s -
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
tried destroying the part after the function is done instead of just removing it but no changes
This is my code
local folder = script.Parent
local Players = game:GetService("Players")
local maxDistance = 50
TouchWait = false
local Remote = game.ReplicatedStorage.Events:WaitForChild("PopupEvent")
-- Follow Player --
function getClosestPlayer(v)
v.CanCollide = false
local closest_player, closest_distance = nil, 100
for i, player in pairs(workspace:GetChildren()) do
if player:FindFirstChild("Humanoid") and game.Players:GetPlayerFromCharacter(player) ~= nil then
local distance = (v.Position - player.PrimaryPart.Position).Magnitude
if distance < closest_distance then
closest_player = player
closest_distance = distance
end
end
end
return closest_player, closest_distance
end
function Loop(v,Movement)
local damping
if v.Name == "Skull" then
damping = 800
else
damping = 1000
end
while true do
local player, distance = getClosestPlayer(v)
if player and distance > 1 then
Movement.Position = player.PrimaryPart.Position
Movement.D = damping
Movement.MaxForce = Vector3.new(4000,10000,4000)
end
if v.Name == "Skull" then
local nearestPlayer, nearestDistance
for _, player in pairs(Players:GetPlayers()) do
local character = player.Character
local distance = player:DistanceFromCharacter(v.Position)
if not character or
distance > maxDistance or
(nearestDistance and distance >= nearestDistance)
then
continue
end
nearestDistance = distance
nearestPlayer = player
end
--look to player's character by IA--
if nearestPlayer then
local char = nearestPlayer.Character
local humanoidRootPart = char:FindFirstChild("HumanoidRootPart")
if humanoidRootPart then
v.CFrame = CFrame.lookAt(v.Position, humanoidRootPart.Position)
end
end
end
task.wait(1)
end
end
function Refresh()
for i, v in pairs(folder:GetChildren()) do
if v:IsA("MeshPart") or v:IsA("Part") then
-- PopUp Event --
v.Touched:connect(function(hit)
if hit.Parent then
if hit.Parent:FindFirstChild("Humanoid")then
local player = game.Players:GetPlayerFromCharacter(hit.Parent)
if player then
if hit.Parent.Humanoid.Health > 0 and v.CanTouch == true then
v.CanTouch = false
v.CanCollide = false
v.Transparency = 1
if v.Name == "Coin" or v.Name == "Coin2" then
v:FindFirstChild("Trail").Enabled = false
local random = math.random(1,900)
local xnew = random/1000
local new = game.ReplicatedStorage.ShowAdd:Clone()
new.Parent = player.PlayerGui.Main
new.Position = UDim2.new(xnew,0,1,0)
if v.Name == "Coin" then
new.Text = "+1💴"
player.playerstats.Coins.Value = player.playerstats.Coins.Value + 1
end
if v.Name == "Coin2" then
v.NumberValue.Value = math.random(5,10)
new.Text = "+" .. (v.NumberValue.Value) .. "💴"
player.playerstats.Coins.Value = player.playerstats.Coins.Value + v.NumberValue.Value
end
local sound = Instance.new("Sound")
sound.SoundId = "rbxassetid://330274138"
sound.Parent = hit
sound:Play()
game.Debris:AddItem(sound,5)
wait(1)
end
if v.Name == "Orb1" or v.Name == "Orb2" or v.Name == "Orb3" or v.Name == "OrbS" then
if player:FindFirstChild("leaderstats3") then
v.emitter:Emit(1)
v:FindFirstChild("ParticleEmitter").Enabled = false
local OrbSound = script:WaitForChild("Sound"):Clone()
OrbSound.Parent = hit.Parent
OrbSound:Play()
game.Debris:AddItem(OrbSound, 4)
if v.Name == "Orb1" then
player.leaderstats3["Total XP"].Value = player.leaderstats3["Total XP"].Value + math.random(10, 20)
end
if v.Name == "Orb2" then
player.leaderstats3["Total XP"].Value = player.leaderstats3["Total XP"].Value + math.random(15, 35)
end
if v.Name == "Orb3" then
player.leaderstats3["Total XP"].Value = player.leaderstats3["Total XP"].Value + math.random(20, 50)
end
if v.Name == "OrbS" then
player.leaderstats3["Total XP"].Value = player.leaderstats3["Total XP"].Value + math.random(1, 5)
end
end
end
if v.Name == "Skull" then
local damage = math.random(1,5)
hit.Parent:FindFirstChild("Humanoid"):TakeDamage(damage)
local emitter = script.Part:Clone()
emitter.Parent = hit.Parent:FindFirstChild("HumanoidRootPart")
emitter.CFrame = v.CFrame
game.Debris:AddItem(emitter, 3)
local sound1 = Instance.new("Sound")
sound1.SoundId = "rbxassetid://3059775781"
sound1.Parent = hit.Parent
sound1:Play()
game.Debris:AddItem(sound1,5)
local sound2 = Instance.new("Sound")
sound2.SoundId = "rbxassetid://7837490457"
sound2.Parent = hit.Parent
sound2:Play()
game.Debris:AddItem(sound2,5)
local sound3 = Instance.new("Sound")
sound3.SoundId = "rbxasset://sounds/uuhhh.wav"
sound3.Parent = hit.Parent
sound3:Play()
game.Debris:AddItem(sound3,5)
end
if v:FindFirstChild("Material") then
v:FindFirstChild("Trail").Enabled = false
player.Materials:FindFirstChild(v.NameValue.Value).Value = player.Materials:FindFirstChild(v.NameValue.Value).Value + 1
Remote:FireClient(player,v.NameValue.Value,v.AssetID.Value)
wait(1)
end
v:Destroy()
end
end
end
if hit.Parent:IsA('Tool') then
local tool = hit.Parent
if tool.Slicing.Value == true and v.Name == "Skull" then
local emitter = script.Part:Clone()
emitter.Parent = workspace
emitter.CFrame = v.CFrame
game.Debris:AddItem(emitter, 2)
local sound2 = Instance.new("Sound")
sound2.SoundId = "rbxassetid://7837490457"
sound2.Parent = hit.Parent
sound2:Play()
game.Debris:AddItem(sound2,5)
v:Destroy()
end
end
end
end)
if not v:FindFirstChildOfClass("BodyPosition") then
local Movement = Instance.new("BodyPosition",v)
Loop(v,Movement)
end
end
end
end
folder.ChildAdded:connect(Refresh)
Nueva imagen de mapa de bits.bmp (341.9 KB)