I'm trying to make a script that mimics temperature by assigning an IntValue to players so it may measure the amount of heat they receive. As shown in the script below, I'm trying to make it so whenever players are within a certain distance of a heat source, their temperature IntValue increases by one, and when they leave the radius, that increase is removed. I thought what I have would work, but it doesn't work as I thought it would.
01 | local Players = game:GetService( "Players" ) |
02 | local player = Players.LocalPlayer or Players:GetPropertyChangedSignal( "LocalPlayer" ):wait() |
03 | local char = player.Character or player.CharacterAdded:Wait() |
04 |
05 | local fire = workspace.Heat |
06 | local warmDist = 12 |
07 |
08 | local temp = game.ReplicatedStorage:WaitForChild(player.Name):WaitForChild( "Temp" ) |
09 |
10 | local inside = false |
11 |
12 | while true do |
13 | wait( 1 ) |
14 | local distance = (char.HumanoidRootPart.Position - fire.Position).magnitude |
15 | if distance < = warmDist then |
I tried to turn the localscript into a normal script yet it still didn't work. Here's what I had with the adjusts I made:
01 | local Players = game:GetService( "Players" ) |
02 | local player = Players.LocalPlayer or Players:GetPropertyChangedSignal( "LocalPlayer" ):wait() |
03 | local char = player.Character or player.CharacterAdded:Wait() |
04 |
05 | local fire = workspace.Heat |
06 | local warmDist = 12 |
07 |
08 | local tempVal = game.ReplicatedStorage:WaitForChild(player.Name):WaitForChild( "Temp" ).Value |
09 | local tempCheck = game.ReplicatedStorage.RemoteEvents.Temperature |
10 |
11 | local inside = false |
12 |
13 | while true do |
14 | wait( 1 ) |
15 | local distance = (char.HumanoidRootPart.Position - fire.Position).magnitude |
So you should try this for a LocalScript, because you need to access LocalScript (it depends, but I'd recommend localscript)
Well I stated in the comment, while true do loop stuck the function to update the temperature. What you are gonna do is like this:
01 | local Players = game:GetService( "Players" ) |
02 | local player = Players.LocalPlayer -- it makes me confuse about using or things in these variables |
03 | local char = player.Character |
04 |
05 | local fire = workspace.Heat |
06 | local warmDist = 12 |
07 |
08 | local tempVal = game.ReplicatedStorage:WaitForChild(player.Name):WaitForChild( "Temp" ).Value |
09 | local tempCheck = game.ReplicatedStorage.RemoteEvents.Temperature |
10 |
11 | while wait( 1 ) do |
12 | local distance = (char.HumanoidRootPart.Position - fire.Position).magnitude |
13 | if distance < = warmDist then |
14 | tempVal = tempVal+ 1 |
15 | else |
16 | tempVal = tempVal- 1 |
17 | end |
18 | end |
I had changed to my own version using the things i stated in comment, also gtg bye i dont know what to make this as an outro so bye bye