Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Scripting Question: Lag optimizer that removes certain components only for the player?

Asked by 3 years ago

I am a dev on a game that includes a sizeable amount of trees on the map. These trees are composed of meshparts, which I understand can cause a substantial amount of lag when used heavily. Would it be possible to create a script that removes said trees from the player's experience, therefore eliminating the lag? Furthermore, I would like to keybind this, for example saying "/removetrees" to complete the desired action. Thank you in advance.

1 answer

Log in to vote
0
Answered by 3 years ago

I don't know how to make the trees disappear, but I can make a script that will fire the client when the command is typed.

SETUP: 1. (OPTIONAL) Create a folder in the replicated storage. Name it whatever you want (I named mine "Events", but it is up to you) 2. Create a remote event and place it in the folder (or just in the replicated storage if you don't want the folder). Name it whatever you want. 3. Create a script inside the "ServerScriptService". Name it whatever you want.

CODE: Now we are going to move on to the code on the script. PLEASE don't copy and paste it then forget about it, it doesn't help you learn. Read it and try your best to understand it.

local commands = {}
local prefix = "/"
commands.removetrees = function(sender,arguements)  --Sends the player that typed the message, and the two players being teleported--
    game.ReplicatedStorage.Folder.Event:Fireclient(sender) --Edit Folder to be the name of the folder (if there isn't a folder, remove Folder and the dot after it) and edit Event to be the name of the event--
    print("fired")
end
game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(message,recipient)
        message = string.lower(message)
        local splitString = message:split(" ")  --Splitting up the message the player sent to see if they were typing a command or not--
        local commandPrefix = splitString[1]    --Getting the command from the message the player typed--
        local cmd = commandPrefix:split(prefix) --Getting rid of the prefix--
        local cmdName = cmd[2]  --The command without a slash (example: I type: "/removetrees" cmdName: "removetrees"--
        if commands[cmdName] then   --Checking if the command is a command--
            local arguements = {}   --Everything after the prefix--
            commands[cmdName](player)   --Fire function--
        else
            print("not a command")
        end
    end)
end)

You can add more settings, all you have to do is copy and paste the removetrees function, change the "removetrees" in the title to the name of the command you want (let's say: "removeparticles") and then add in the event and change it to fire the event for that (instead of it firing "TreeEvent", it will fire "ParticleEvent".

Ad

Answer this question