I want to make a spleef script, where the player has to use a shovel to click specific plates. I did this by making a local script that detects if a player has clicked a part, and if so, it will fire a remote event. This remote event, upon firing, will start another script that deals with making a clicked plate disappear. This script works. However, when I click, my frames go from 60-61 to even 35.
How can I stop my frame rate from lowering whenever I click?
LocalScript:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local tiles = workspace.TilesTest
local shovel = script.Parent
local DB = false
local ReS = game:GetService("ReplicatedStorage")
local Dig = ReS:WaitForChild("DigEvent")
for _, SelectedPart in pairs(tiles:GetChildren("Platform")) do
shovel.Activated:Connect(function()
if mouse.Target == SelectedPart then
if DB == false then
if (player.Character.Torso.Position-mouse.Target.Position).magnitude < 35 then
DB = true
Dig:FireServer(SelectedPart)
wait(.25)
DB = false
end
end
end
end)
end
ServerScript:
local TS = game:GetService("TweenService")
local RepS = game:GetService("ReplicatedStorage")
local Dig = RepS:WaitForChild("DigEvent")
local Fade = 3
local function tween(t)
local T1 = TS:Create(t, TweenInfo.new(Fade, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Transparency = 1})
local T2 = TS:Create(t, TweenInfo.new(Fade, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {Position = t.Position - Vector3.new(0,1.7,0)})
T1:Play()
T2:Play()
end
Dig.OnServerEvent:Connect(function(plr, tile)
if tile:GetAttribute("Pressed") == false then
tile:SetAttribute("Pressed", true)
tween(tile)
wait(Fade)
tile.CanCollide = false
else
return
end
end)```
I would assume the fps drops are because youre using a very unoptimized way of locating what part the mouse is on.
This entire function should be replaced with this, which shouldnt lag at all.
This code should function.
local RaycastBlacklist = {}
function UpdateRaycastBlacklist()
table.clear(RaycastBlacklist)
table.insert(RaycastBlacklist, Character)
for i, v in pairs(game.Players:GetPlayers()) do
if v.Character then
table.insert(RaycastBlacklist, v.Character)
end
end
end
shovel.Activated:Connect(function()
local RayParams = RaycastParams.new()
UpdateRaycastBlacklist()
RayParams.FilterDescendantsInstances = RaycastBlacklist
local RayDirection = (Camera.CFrame.Position - Mouse.Hit.Position).Unit * -500
local Result = workspace:Raycast(Camera.CFrame.Position, RayDirection, RayParams)
if Result and Result.Position and Result.Instance and Result.Instance.Parent == tiles and (player.Character.Torso.Position - Result.Position).magnitude < 35 and not DB then
DB = true
Dig:FireServer(SelectedPart)
wait(.25)
DB = false
else
print('No part was hit')
end
end)
It doesnt work for me, instead it lags harder and it doesnt even erase some of the plates. unless im doing something wrong:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local tiles = workspace.TilesTest
local shovel = script.Parent
local DB = false
local ReS = game:GetService("ReplicatedStorage")
local Dig = ReS:WaitForChild("DigEvent")
local Camera = workspace.CurrentCamera
local RaycastBlacklist = {}
function UpdateRaycastBlacklist()
table.clear(RaycastBlacklist)
table.insert(RaycastBlacklist, Character)
for i, v in pairs(game.Players:GetPlayers()) do
if v.Character then
table.insert(RaycastBlacklist, v.Character)
end
end
end
for _, SelectedPart in pairs(tiles:GetChildren("Platform")) do
shovel.Activated:Connect(function()
local RayParams = RaycastParams.new()
UpdateRaycastBlacklist()
RayParams.FilterDescendantsInstances = RaycastBlacklist
local RayDirection = (Camera.CFrame.Position - mouse.Hit.Position).Unit * -500
local Result = workspace:Raycast(Camera.CFrame.Position, RayDirection, RayParams)
if Result and Result.Position and Result.Instance and Result.Instance.Parent == tiles and (player.Character.Torso.Position - Result.Position).magnitude < 35 and not DB then
DB = true
Dig:FireServer(SelectedPart)
wait(.25)
DB = false
else
print('No part was hit')
end
end)
end
You dont need this line at all, and it is what im assuming is causing the lag.
What youre doing in that function is that for each part on your spleef area, you wait for the tool to be used, and once its been used, you fire the event for each part.
So, for example, theres 150 parts. One click will be 150 function calls.
Could just be a raycasting issue, try printing the result after you raycast. print(Result, Result.Instance, Result.Instance.Parent)
But to reduce lag, remove the for_, SelectedPart loop, and just keep the tool.Activated function.
Also, im using that exact raycasting code, and firing it once every frame at 120 FPS, and im not getting any frame drops.
The better performance could be because im using an RTX2060, but i doubt that as the same code worked fine on an 11 year old computer i have.
you were right! it was the for _, loop that slowed the frames. the raycasting seems to work perfectly, but im not sure what to put for SelectedPart though.
oh yes and uh, how do i make it so that i can click the plate again after the round? I intended for the plates to regenerate and be able to be clicked again after a few minutes/seconds. which is why i used a bool attribute to do that. however the script you gave me cannot be repeated and can only be done once. is there a way i can make it a repeatable action (after a course of time?)