[SOLVED] How could I add a BoolValue inside a script?

Hello, I have this Sun damage script, that deals damage to the player when touches the Sun and is safe on shadows.

I’m trying to add a BoolValue that when is Enabled you don’t get damage in the Sun.

12

Where BoolValue is located.

My code

-- Sun script is located in the same folder as Immunity.

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

-- PlayerStats is the folder inside StarterCharacterScripts where the strings and the boolvalue is located.

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
		
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:FindPartOnRay(ray, char)
			
	if partFound then
		safe = true
		Safe()
	else
	    safe = false
	    coolDown = math.max(0, coolDown - deltaTime)
			if coolDown <= 0 then
				if burning == false then
				   burning = false
				   BurnPlayer()
				else
				coroutine.yield()
			end
		end
	end
end)
	
game.Lighting:GetPropertyChangedSignal("TimeOfDay"):Connect(function()
sunDirection = game.Lighting:GetSunDirection()
end)

A server-script cannot access game.Players.LocalPlayer. Consider making sure workspace.RejectCharacterDeletions is true and put this server-script in StarterCharacterScripts.

-- Sun script is located in the same folder as Immunity.

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

-- PlayerStats is the folder inside StarterCharacterScripts where the strings and the boolvalue is located.

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
		
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:FindPartOnRay(ray, char)
			
	if partFound then
		safe = true
		Safe()
	else
	    safe = false
	    coolDown = math.max(0, coolDown - deltaTime)
			if coolDown <= 0 then
				if burning == false then
				   burning = false
				   BurnPlayer()
				else
				coroutine.yield()
			end
		end
	end
end)
	
game.Lighting:GetPropertyChangedSignal("TimeOfDay"):Connect(function()
sunDirection = game.Lighting:GetSunDirection()
end)
1 Like

Hello, yeah I tried it and works exactly like before, the reason I added game.Players.LocalPlayer, is because I have a script disabled in other system for this one, like a race, if they are in a determined race the script disables or enables and works perfectly.

What I’m trying to do is add the boolvalue “SunImmunity”, is a value of a clan perk, because if you are in a race that can burn in the sun I’m trying to add this value, that if you are in the determined clan, you get the sunimmunity.

Did you change workspace.RejectCharacterDeletions to Enabled and put the script in StarterCharacterScripts?

You could just add a statement to check the value.

if not burning and char.SunImmunity.Value == false then
    burning = false
    BurnPlayer()
end

I would also like to point out that workspace:FindPartOnRay is deprecated; you should use workspace:Raycast instead.

1 Like

They could just set the script’s RunContext to client; but even then, if it is a LocalScript, it should work as a descendant of the character. Either way, it should run.

Hello, thanks you, I added the value and it worked correctly.

About the workspace:Raycast if I add it it gives me an error of vector 3, so I keep the workspace:FindPartOnRay.

That’s because you are trying to cast with the ray instance. To convert it, you would just cast from the ray’s origin and direction.

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

local partFound = workspace:Raycast(ray.Origin, ray.Direction * 15, params)

Or, just cast using the origin and direction given to Ray.new. Make sure to multiply the direction to reach the length you want the ray to be.

Since workspace:FindPartOnRay is deprecated, it is no longer updated and will eventually become non-functional. Converting it now might save you a headache later on.

1 Like

Thanks I fixed it now, I have another topic if you are interested I can give you the link.

1 Like

Here is the link if you are interested. :+1:

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