[SOLVED] How do I server side a local script?

I have this Sun damage script, is a LocalScript, it works correctly in local but others players can’t see the HealthBar going down when getting damaged, only local.

All players get damaged by the sun and get killed without issues, the problem is that the HealthBar is only local.

Where the LocalScript is located.

1

The character is inside a folder in Workspace.

2

Player is getting damaged but the HealthBar doesn’t go down, only in local.

3

My code

local char = game.Players.LocalPlayer.Character
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()
				else
				coroutine.yield()
			end
		end
	end
end)
	
game.Lighting:GetPropertyChangedSignal("TimeOfDay"):Connect(function()
sunDirection = game.Lighting:GetSunDirection()
end)

You could use a RemoteEvent inside the burn function, and have TakeDamage occur on the server.

2 Likes

Okay let me explain this… it’s gonna be a long explanation, just bare with it

Okay so this is clearly a LocalScript (you’re getting the Character with game.Players.LocalPlayer)

You’re damaging the player ON THE CLIENT and so the health change is only visible on the client

And when the player’s health is at 0, it’ll die right? Roblox trusts the client that they died and so it’ll register the player on the server as dead too

You’d probably need to send a remote to the server after you’d detected that they’re in the sun (not very effective I know)

The bonus for sending a remote is that you get to do a check on the server too to verify (although on the server there’s a bit of a offset in positions compared to the client)

1 Like

Thanks you I’m going to try to add a RemoveEvent, I’m don’t know much about Coding but I will try my best.

Yeah it seems I need a RemoteEvent, I’m going to try to add one inside the LocalScript.

1 Like

The only problem is you’re running this via RenderStepped, your game’s performance is gonna go down significantly

I don’t know how you’d be able to accomplish this without a RemoteEvent

Y’know what you could do is a check to make sure a player has take like 20 damage since you’ve last saved their health, and if that’s true then you can send a remote then

I’m doing it with a RemoveEvent, but I have a question where should I place the FireServer()? I mean in what part of the script, I don’t know much about coding.

1 Like

you’d place it here I guess since this is where you’re taking damage

Is this possibly for a When Day Breaks game? Kinda seems like it with the sun burning thing

I added the sun damage because I have races inside the game, some are able to walk under sun light and others no, the “SunImmunity” is an ability that you can walk under light even if you are in a race that can’t.

The code

local char = game.Players.LocalPlayer.Character
local Immunity = char.PlayerStats:WaitForChild("SunImmunity") -- boolvalue, when enabled The player doesn't get sun damage.
local Event = game.ReplicatedStorage:WaitForChild("SunEvent")

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
				Event.SunEvent:FireServer()
				burning = false
				BurnPlayer()
				else
				coroutine.yield()
			end
		end
	end
end)
	
game.Lighting:GetPropertyChangedSignal("TimeOfDay"):Connect(function()
sunDirection = game.Lighting:GetSunDirection()
end)

Is not working right now, but as I said before I’m still new in coding.

This appers in console.

I also added a Server Script in “ServerScriptService”

local Event = game.ReplicatedStorage:WaitForChild("SunEvent")

Event.OnServerEvent:Connect(function(player)
	
end)
1 Like

Think ya made a mistake here, event is already defined as “SunEvent” here

So you’d basically do this instead:

Event:FireServer()

yeah just do the same thing you did in your BurnPlayer function

1 Like

Alright the console error is gone, in the ServerSide script do I copy this?

function BurnPlayer()
	char.Humanoid:TakeDamage(10)
		burning = false
		coolDown = coolDownDuration
end
		
function Safe()
     if burning  == true then
		burning = false
	end
end
1 Like

The safe function might be a bit unnecessary but yeah

1 Like

Something like this?

local Event = game.ReplicatedStorage:WaitForChild("SunEvent")
Event.OnServerEvent:Connect(function(BurnPlayer, Safe)
local char = game.Players.LocalPlayer.Character

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
	
end)

I got this in console.

1 Like

Yeah but you need to call the function

Burn()

Also, if you’re putting the function in the OnServerEvent scope (inside the function) then there’s no point in declaring it. Here’s what I mean:

local Event = game.ReplicatedStorage:WaitForChild("SunEvent")
Event.OnServerEvent:Connect(function(BurnPlayer, Safe)
local char = BurnPlayer.Character

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

		char.Humanoid:TakeDamage(10)
		burning = false
		coolDown = coolDownDuration	
end)
1 Like

You cant get a LocalPlayer in a server script. You’re gonna need to get the player like this:

Event.OnServerEvent:Connect(function(Player)

The player parameter is provided automatically when you fire to the server

You can get the character from there

I just realized that you’re already doing that with the “BurnPlayer” parameter so you can just do that

1 Like

Something like this?

local Event = game.ReplicatedStorage:WaitForChild("SunEvent")
Event.OnServerEvent:Connect(function(Player, burning)
local char = Player.Character

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

	char.Humanoid:TakeDamage(10)
	burning = false
	coolDown = coolDownDuration	
end)
1 Like

Yeah but now those variables at the bottom are kinda useless since you don’t need them for anything

--Script inside StarterPlayer.StarterCharacterScripts
local char = script.Parent
--rest of your code

It’s better if you use this instead of remotes, else if you don’t set up checks exploiters will be able to decrease the damage rate or even remove the damage by simply not firing the event.

3 Likes

It doesn’t work inside studio, but in a server test it does, I’m playing in game right now and It doesn’t work.