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.
The character is inside a folder in Workspace.
Player is getting damaged but the HealthBar doesn’t go down, only in local.
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)
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)
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.
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.
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
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)
--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.