Help with CastShadow performance script

Hey everyone! I’m trying to createa a script that disables a specific MeshPart CastShadow propertie if it reaches a certain distance, but for some reason my script isn’t working. The CastShadow is not being disabled or enabled.

local player = game.Players.LocalPlayer
local distance = 10 
local partName = "MeshTest" 

local function checkDistance(part)
	local partPos = part.Position
	local playerPos = player.Character.HumanoidRootPart.Position
	local dist = (partPos - playerPos).Magnitude
	return dist
end

local function checkAndToggle()
	local parts = game.Workspace:GetDescendants()
	for _, part in pairs(parts) do
		if part:IsA("MeshPart") and part.Name == partName and checkDistance(part) < distance then
			part.CastShadow = false
		elseif part:IsA("MeshPart") and part.Name == partName and checkDistance(part) >= distance then
			part.CastShadow = true
		end
	end
end

game:GetService("RunService").Heartbeat:Connect(checkAndToggle)

The localscript is located at the StarterPlayerScripts. There’s no errors in the output.
Any help would be highly appreciated. Thank you for your time!

There are a few potential issues with your code that could be causing it to not function as expected.

  1. You are using the RunService.Heartbeat event to check the distance between the player and the MeshPart every frame. This could potentially cause the code to run very frequently and potentially affect the performance of your game. Consider using a different event, such as PlayerAdded or PlayerMovement , to check the distance less frequently.
  2. The checkDistance function calculates the distance between the partPos and playerPos , but it is not being used to update the distance variable that is being compared to in the checkAndToggle function.
  3. In the checkAndToggle function, you are using GetDescendants to get all of the descendants of the Workspace . This includes all parts, models, and other objects in the Workspace , which could potentially be a very large number. This could potentially cause the code to run slowly and affect the performance of your game. Consider using a more specific method to find the specific MeshPart you are interested in, such as using FindFirstChild or GetChildren on a specific model.
  4. It is also possible that the CastShadow property of the MeshPart is not being updated because it is being overridden by another script or by a setting in the MeshPart 's properties.

I generated this response with https://chat.openai.com/chat, let me know if you have any further questions and I’ll give the AI that. I advice you to test the points mentioned and then see if any of them causes the issues.

If this is running from the client, you should use .RenderStepped not .Heartbeat.

Secondly, it would be easier if you just did if part:IsA'BasePart' then instead of if part:IsA'MeshPart' then.

Also, make sure that the partName value is correct. Look for misspelling in the workspace or in your script.

Considering it ran just fine, it’s most likely an issue involving the instance name.

Your code is good, but the only problem is that the local script is located in the StarterPlayerScripts folder, which is not replicated to the client. You need to move the local script to the ServerScriptService, and then remove the local player = game.Players.LocalPlayer and change player.Character.HumanoidRootPart.Position to player.Character.HumanoidRootPart.Position+(player.Character.HumanoidRootPart.CFrame.lookVector*-0.5)