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

Why wont the current value change?

Asked by
BiIinear 104
5 years ago

This has been really annoying for me. When I press E on an object that has an "Interact" value, a remote event fires, as seen on here.

Local Script:

InputService.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.E then
        if not DragTarget and Mouse.Target ~= nil then
            local t = Mouse.Target
            if t.Parent and t:FindFirstChild("Interact") then
                if t:FindFirstChild("Interact").Value == true then
                    local interact = t:FindFirstChild("Interact").Parent
                    game.ReplicatedStorage.RemoteEvent:FireServer(interact)
                end
            end
        end
    end
end)

In the server script, the object is supposed to play a sound. What it's doing here is that when the event is fired, it checks the targets name. The next thing it does is that it changes the "current" value to the next sound. But that won't work, apparently the current value always stays at radio1, I have no idea why. When the event is fired the current value is supposed to change to the next sound (sorry if I repeated this.)

Server Script:

game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(plr, interact)
    if interact.Parent.Name == "Radio" then

        local model = interact.Parent
        local radio = model.Radio
        local power = model.Power
        local backward = model.Backward
        local forward = model.Forward
        local debounce = false
        local on = true

        local radio1 = radio.radio01
        local radio2 = radio.radio02
        local radio3 = radio.radio03
        local radio4 = radio.radio04
        local radio5 = radio.radio05
        local radio6 = radio.radio06
        local radio7 = radio.radio07
        local radio8 = radio.radio08
        local radio9 = radio.radio09
        local radio10 = radio.radio10

        local current = radio1

        if interact.Name == "Backward" then
            if debounce == false then
                debounce = true
                if on == true then
                    if current == radio1 then current.Volume = 0 current = radio10 current.Volume = .5
                    elseif current == radio2 then current.Volume = 0 current = radio1 current.Volume = .5
                    elseif current == radio3 then current.Volume = 0 current = radio2 current.Volume = .5
                    elseif current == radio4 then current.Volume = 0 current = radio3 current.Volume = .5
                    elseif current == radio5 then current.Volume = 0 current = radio4 current.Volume = .5
                    elseif current == radio6 then current.Volume = 0 current = radio5 current.Volume = .5
                    elseif current == radio7 then current.Volume = 0 current = radio6 current.Volume = .5
                    elseif current == radio8 then current.Volume = 0 current = radio7 current.Volume = .5
                    elseif current == radio9 then current.Volume = 0 current = radio8 current.Volume = .5
                    elseif current == radio10 then current.Volume = 0 current = radio9 current.Volume = .5
                    end
                    backward.BrickColor = BrickColor.new("Sea green")
                    print(current)
                    wait(0.1)
                    backward.BrickColor = BrickColor.new("Persimmon")
                end
                debounce = false
            end
        end
        if interact.Name == "Forward" then
            if debounce == false then
                debounce = true
                if on == true then
                    if current == radio10 then current.Volume = 0 current = radio1 current.Volume = .5
                    elseif current == radio9 then current.Volume = 0 current = radio10 current.Volume = .5
                    elseif current == radio8 then current.Volume = 0 current = radio9 current.Volume = .5
                    elseif current == radio7 then current.Volume = 0 current = radio8 current.Volume = .5
                    elseif current == radio6 then current.Volume = 0 current = radio7 current.Volume = .5
                    elseif current == radio5 then current.Volume = 0 current = radio6 current.Volume = .5
                    elseif current == radio4 then current.Volume = 0 current = radio5 current.Volume = .5
                    elseif current == radio3 then current.Volume = 0 current = radio4 current.Volume = .5
                    elseif current == radio2 then current.Volume = 0 current = radio3 current.Volume = .5
                    elseif current == radio1 then current.Volume = 0 current = radio2 current.Volume = .5
                    end
                    forward.BrickColor = BrickColor.new("Sea green")
                    print(current)
                    wait(0.1)
                    forward.BrickColor = BrickColor.new("Persimmon")
                end
                debounce = false
            end
        end
        if interact.Name == "Power" then
            if debounce == false then
                debounce = true
                if on == true then
                    on = false
                    power.BrickColor = BrickColor.new("Persimmon")
                    current.Volume = 0
                else
                    on = true
                    power.BrickColor = BrickColor.new("Sea green")
                    current.Volume = .5
                end
                debounce = false
            end
        end
    end
end)

PS: No errors print out. Help would be appreciated.

1 answer

Log in to vote
1
Answered by
Nefarioum 162
5 years ago

The code is very messy and there are a lot of repeated things going on which makes it quite hard to understand what is going on however every time I read through it I notice the same thing - every single time the remote is fired it forces the variable Current which is declared in line 23 to Radio1.

Think of it this way: no matter if they are changing the radio to the next station or the previous station - when the remote is fired, it will always set it to radio1 again. You need to store the variable outside of the scope of the OnServerEventso that when it is changed it is stored. However, keep in mind that doing this will mean every radio will have the same station.

To fix this, you'll need to create a table and store the values of the current song and have a dedicated value for each of the stations. I have created an example of how this would look like:

local CurrentSongs = {
        ["Radio1"] = "SongHere",
        ["Radio2"] = "SongHere"
}

print(CurrentSongs.Radio1) -- To access it simply grab the radio name which is fired by the client and find the dedicated table entry in the CurrentSongs table

Keep in mind that this has to be outside and above of the OnServerEvent. However, if there is only 1 radio capable of playing music in the game you do not need a table and can just stick with a variable declared outside the scope of the OnServerEvent. Sorry if this has been quite confusing - I hope you get the gist of what I'm saying and if not you can leave a comment and I'll give you further support.

0
tl;dr? i couldnt wrap my head around that, very sorry BiIinear 104 — 5y
0
Line 23, move it outside of the OnServerEvent. Everytime the remote is fired, it sets it back to radio1. Nefarioum 162 — 5y
0
ok now it makes sense, im an idiot. didnt notice that lol BiIinear 104 — 5y
0
It happens to the best of us, all the best. Nefarioum 162 — 5y
Ad

Answer this question