Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I add multiple radioactive sources in my geiger counter script?

Asked by 5 years ago
Edited 5 years ago

Hello everyone,

As this is my first post here, please bear with me if I am unclear or do something wrong. I hope I am doing this the right way. Also, please note that I am asking about a concept and a method. This is not a request, or anything similar to that.

So, I have recently picked up coding in Roblox, and so far I think it is pretty fun. Right now I am working on a geiger counter tool. The tool measures the distance from the player to a RadiationSource (a Part) and uses that to calculate the output of the geiger counter. The RadiationSource has two NumberValues that store the activity (how radioactive) and the range for the radiation. The following LocalScript is inside of the tool, along with a ScreenGui, Handle, and Script that opens and closes the ScreenGui when the tool is equipped/unequipped.

repeat wait() until game.Players.LocalPlayer.Character

while true do
    local player = game.Players.LocalPlayer
    local gui = player:WaitForChild("PlayerGui")
    local ui = gui:WaitForChild("Meter")

    local position1 = workspace.RadiationSource.Position
    local position2 = game.Players.LocalPlayer.Character.HumanoidRootPart.Position
    local distance = (position1 - position2).magnitude

    local activity = workspace.RadiationSource.Activity.Value
    local range = workspace.RadiationSource.Range.Value
    local doserate = activity/(distance*distance)

    local radiation = doserate + (math.random(-40, 40)/1000)
    local background = 0.2 + (math.random(-40, 40)/1000)

    if distance <= range and radiation > 0.2 then
        local output = ((radiation + 0.005) - (radiation + 0.005) % 0.01)
        ui.Frame.txtDoserate.Text = output.." uSv/h"

        if output < 0.3 then
            ui.Frame.txtWarning.Text = "Normal"
        elseif output > 0.3 and radiation < 0.6 then
            ui.Frame.txtWarning.Text = "Elevated"
        elseif output > 0.6 and radiation < 2 then
            ui.Frame.txtWarning.Text = "High"
        elseif output > 2 and radiation < 30 then
            ui.Frame.txtWarning.Text = "DANGER"
        elseif output > 30 then
            ui.Frame.txtWarning.Text = "DANGER!!!"
        end
    else
        local output = ((background + 0.005) - (background + 0.005) % 0.01)
        ui.Frame.txtDoserate.Text = output.." uSv/h"

        if output < 0.3 then
            ui.Frame.txtWarning.Text = "Normal"
        elseif output > 0.3 and radiation < 0.6 then
            ui.Frame.txtWarning.Text = "Elevated"
        elseif output > 0.6 and radiation < 2 then
            ui.Frame.txtWarning.Text = "High"
        elseif output > 2 and radiation < 30 then
            ui.Frame.txtWarning.Text = "DANGER"
        elseif output > 30 then
            ui.Frame.txtWarning.Text = "DANGER!!!"
        end
    end

    wait (1)
end

The code works great, but only with one RadiationSource. What I would like for my tool to do is work with multiple RadiationSources. So, I am thinking of modifying my code so that it does this: - For every RadiationSource (Part) in Workspace it checks its range (a NumberValue in the Part). - If the player is within range, it adds that RadiationSource's doserate (which depends on that RadiationSource's NumberValue called activity and that Part's distance to player) to a sum of all other doserates from all other RadiationSources within range. This sum of doserates from multiple RadiationSources is then the final radiation at that position. - The sum of doserates should perhaps be reset after each refresh of the geiger counter's output.

As I am relatively new to scripting, I am not quite sure what method to use if I wanted to locate all RadiationSources in the Workspace and do what I described with those. What method could I use in the script so that my Geiger Counter can detect multiple RadiationSources? How would one go about figuring out a possible way to make this concept work?

I hope that someone might have an idea of a possible method that I could use to complete my geiger counter tool.

Thank you all, Acro

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Hello again,

I managed to figure out a solution:

repeat wait() until game.Players.LocalPlayer.Character

while true do
    wait (1)

    local player = game.Players.LocalPlayer
    local gui = player:WaitForChild("PlayerGui")
    local ui = gui:WaitForChild("Meter")

    local sources = game.Workspace.Radioactive:GetChildren()

    doseratesum = 0

    for i = 1, #sources do
        local source = sources[i]

        local position1 = source.Position
        local position2 = game.Players.LocalPlayer.Character.HumanoidRootPart.Position
        local distance = (position1 - position2).magnitude

        local activity = source.Activity.Value
        local range = source.Range.Value

        if distance <= range then
            local doserate = activity / (distance * distance)
            if doserate > 0.2 then
                doseratesum = doseratesum + doserate
            end
        end
    end

    local radiation = doseratesum + (math.random(-40, 40)/1000)
    local background = 0.2 + (math.random(-40, 40)/1000)

    if doseratesum > 0.2 then
        local output = ((radiation + 0.005) - (radiation + 0.005) % 0.01)
        ui.Frame.txtDoserate.Text = output.." uSv/h"

        if output < 0.3 then
            ui.Frame.txtWarning.Text = "Normal"
        elseif output > 0.3 and output < 0.6 then
            ui.Frame.txtWarning.Text = "Elevated"
        elseif output > 0.6 and output < 2 then
            ui.Frame.txtWarning.Text = "High"
        elseif output > 2 and output < 30 then
            ui.Frame.txtWarning.Text = "DANGER"
        elseif output > 30 then
            ui.Frame.txtWarning.Text = "DANGER!!!"
        end
    else
        local output = ((background + 0.005) - (background + 0.005) % 0.01)
        ui.Frame.txtDoserate.Text = output.." uSv/h"

        ui.Frame.txtWarning.Text = "Normal"
    end
end

The solution includes putting all RadiationSources in a model called Radioactive.

Thanks, Acro

0
I am sorry, I don't understand your question. AcroTerion 0 — 5y
0
Maginatoc is just dumb. You should edit your answer to contain the solution. It will help future developers who find your question, who might have the same issue you were having. You might even get up votes if people like your solution. User#24403 69 — 5y
0
Okay, I will edit my answer. Thanks! AcroTerion 0 — 5y
0
I never replied to this thread @inca Maginatoc -5 — 5y
Ad

Answer this question