[SOLVED] Server Script working in Local Server, but not in game or client

I have this Sun damage script, that is actually a Server side script, it works in Server but not in local (Sometimes).

This is where the script is located.

1

This is what I mean.

I tested it in a Local Server in studio and it works perfectly, if I play in studio the video above happens and if I play in game nothing happens, I don’t get damage.

My code

local char = script.Parent
local Immunity = char.PlayerStats:WaitForChild("SunImmunity") -- boolvalue, when enabled The player doesn't get sun damage.

local sunDirection = game.Lighting:GetSunDirection()

local sun_Detect = 1000
local coolDown = 0
local coolDownDuration = 3

local safe = true
local burning = false

function BurnPlayer()
	char.Humanoid:TakeDamage(10)
	burning = false
	coolDown = coolDownDuration
end

function Safe()
	if burning  == true then
		burning = false
	end
end

local params = RaycastParams.new()
params.FilterDescendantsInstances = {char}
params.FilterType = Enum.RaycastFilterType.Exclude

game:GetService("RunService"):BindToRenderStep("SunService", Enum.RenderPriority.Camera.Value + 1, function(deltaTime)
	local ray = Ray.new(char.HumanoidRootPart.Position, sunDirection * sun_Detect)
	local partFound = workspace:Raycast(ray.Origin, ray.Direction * 15, params)

	if partFound then
		safe = true
		Safe()
	else
		safe = false
		coolDown = math.max(0, coolDown - deltaTime)
		if coolDown <= 0 then
			if not burning and char.PlayerStats.SunImmunity.Value == false then
				burning = false
				BurnPlayer()
			end
		end
	end
end)

game.Lighting:GetPropertyChangedSignal("TimeOfDay"):Connect(function()
	sunDirection = game.Lighting:GetSunDirection()
	
end)
  1. Any errors?
  2. Have you tried adding print statements or adding breakpoints?
  3. There is no point in doing coroutine.yield in this case, the function runs every RenderStep.

I don’t get any console errors, and nope I didn’t tried anything else.

I removed the coroutine.yield is still the same, the script works perfectly in Server side, but in client doesn’t work.

In studio you are both client and server at the same time, so the BindToRenderStep process goes through. Unfortunately, such a trick will not work on a real server.

You can only use RunService.RenderStepped or RunService:BindToRenderStep on the client. Switch to RunService.Heartbeat instead.

Something like this?

game:GetService("RunService"):Heartbeat("SunService", Enum.RenderPriority.Camera.Value + 1, function(deltaTime)
	local ray = Ray.new(char.HumanoidRootPart.Position, sunDirection * sun_Detect)
	local partFound = workspace:Raycast(ray.Origin, ray.Direction * 15, params)

Yeah that is the problem I have.

No, like this:

game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
	local ray = Ray.new(char.HumanoidRootPart.Position, sunDirection * sun_Detect)
	local partFound = workspace:Raycast(ray.Origin, ray.Direction * 15, params)

	if partFound then
		safe = true
		Safe()
	else
		safe = false
		coolDown = math.max(0, coolDown - deltaTime)
		if coolDown <= 0 then
			if not burning and char.PlayerStats.SunImmunity.Value == false then
				burning = false
				BurnPlayer()
			end
		end
	end
end)
local RunService = game:GetService("RunService")

-- triggers after physics
RunService.PostSimulation:Connect(function(DeltaTime: number)
  -- code
end)

Is working right now, thanks you.

Thanks you for the help anyways.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.