#Help With Lag Spike in code

1 messages · Page 1 of 1 (latest)

turbid pumice
#

ok so i made this code local pointPart = script.Parent

local blue = Color3.fromRGB(0, 0, 255)
local green = Color3.fromRGB(0, 255, 0)
local red = Color3.fromRGB(255, 0, 0)

local smallPoints = 10
local bigPoints = 50
local loosePoints = 100

local Players = game:GetService("Players")

local function givingPoints(player)
local currentColor = pointPart.Color

local playerStats = player:FindFirstChild("leaderstats")
if not playerStats then return end

local playerpoints = playerStats:FindFirstChild("Points")
if not playerpoints then return end

if currentColor == blue then
    playerpoints.Value += smallPoints
elseif currentColor == green then
    playerpoints.Value += bigPoints
else
    playerpoints.Value -= loosePoints
end

pointPart.Transparency = 1
pointPart.CanCollide = false
pointPart.CanTouch = false

local particle = Instance.new("ParticleEmitter")
particle.Color = ColorSequence.new(currentColor)

print("Added particle")

local playerCharacter = player.Character
particle.Parent = playerCharacter:WaitForChild("Head")

task.wait(1)

print("Destroying")

particle:Destroy()
pointPart:Destroy()

end

local function partTouched(otherPart)
local player = Players:GetPlayerFromCharacter(otherPart.Parent)
if player then
givingPoints(player)
end
end

pointPart.Touched:Connect(partTouched)

task.spawn(function()
while true do
pointPart.Color = blue
task.wait(2)
pointPart.Color = green
task.wait(2)
pointPart.Color = red
task.wait(2)
end
end)
and it works but everytime i tuch the part to get points i get lag spikes and i have no clue why

stark delta
#

its bc the script is inside the part

#

I just tested it

#

bc u destroy the part

#

so will be the script too

#

cuz its a child of it

turbid pumice
#

how do i change that

stark delta
#

u just change the scripts parent..?

#

like it shouldnt be inside the part

#

since you are destroying it

#

you did a beginner mistake

#

you shouldnt put scripts inside parts

#

they should be saved inside serverscriptservice

#

@turbid pumice

turbid pumice
#

Ok so i put the script in server script service and instead of script.parent i change it to workspace.pointpart ?

stark delta
#

yea

#

but

#

:WaitForChild("Part")

#

or Pointpart

red verge
#

imo :WaitForChild is better than Pointpart in this scenario

barren dock
turbid pumice
#

ok im confused

stark delta
#

bro

#

just change the scripts parent

#

thats it