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

Voting Script Won't Work. Can anyone help me fix it?

Asked by
Discern 1007 Moderation Voter
9 years ago

I've tried everything to fix this, but nothing works. There is no output. This is how the objects in the script look like in Explorer:

Frame

-Minigame1 (TextButton, it's text is "Minigame1". It's BackgroundColor3 is 255,255,255.)

- -Problem Script (The script that I'm trying to fix.)

- -Voted (BoolValue, It's value is False.)

-Minigame2 (TextButton, it's text is "Minigame2". It's BackgroundColor3 is 255,255,255.)

- -Problem Script (The script that I'm trying to fix.)

- -Voted (BoolValue, It's value is False.)

-Minigame3 (TextButton, it's text is "Minigame3". It's BackgroundColor3 is 255,255,255.)

- -Problem Script (The script that I'm trying to fix.)

- -Voted (BoolValue, It's value is False.)

Workspace

-Minigame1Votes (IntValue, it's value is 0.)

-Minigame2Votes (IntValue, it's value is 0.)

-Minigame3Votes (IntValue, it's value is 0.)

This is the problem script in the Buttons:

chosen = script.Parent:WaitForChild("Voted")

script.Parent.MouseButton1Click:connect(function()
    if chosen.Value == false and script.Parent.BackgroundColor3 ~= Color3.new(0,1,0) then
        chosen.Value = true
        script.Parent.BackgroundColor3 = Color3.new(0,1,0)
        game.Workspace["" ..script.Parent.Text.. "Votes"].Value = game.Workspace["" ..script.Parent.Text.. "Votes"].Value + 1
        local lastvoted = nil
        for i,v in pairs(script.Parent.Parent:GetChildren()) do
            if v:IsA("TextButton") then
                if v.Voted.Value == true then
                    v.BackgroundColor3 = Color3.new(1,1,1)
                    v.Voted.Value = false
                    lastvoted = v
                end
            end
        end
        if lastvoted then
            if game.Workspace["" ..lastvoted.Text.. "Votes"].Value > 0 then
                game.Workspace["" ..lastvoted.Text.. "Votes"].Value = game.Workspace["" ..lastvoted.Text.. "Votes"].Value - 1
            end
        end
    end
end)

The button does not change color, nor do the values in Workspace change.

0
Is there any output? VariadicFunction 335 — 9y
0
I have it in the first sentence already. "There is no output." In bold text... Discern 1007 — 9y
0
Try adding print methods to the script and see where it stops outputting messages. Post the modified script and outputted messages here. Spongocardo 1991 — 9y
0
I've tried that before. I put prints between every single line in the script (Including the ends and such), and it printed every single one into the Output. Discern 1007 — 9y
View all comments (4 more)
0
Well what's your goal, what's not working right, etc. Please explain your questions. Perci1 4988 — 9y
0
@Perci, if the script's parent isn't voted when it's clicked, then it votes for it by making the script's parent green and setting its "Voted" value to true. It changes the other 2 buttons to white (255,255,255) and it's Voted value to false. Their texts are "Minigame1", "Minigame2", and "Minigame3". There are 3 IntValues in workspace. "Minigame1Votes", "Minigame2Votes", and "Minigame3Votes". When Discern 1007 — 9y
0
a button is clicked, let's say Minigame1 is clicked, the button turns green and "Minigame1Votes" in Workspace goes up. It unvotes any other ones that were already voted for (Makes when white again and subtracts one from the value in Workspace) Discern 1007 — 9y
0
The problem is, no buttons are turning green/white, and the values in Workspace don't change. It's almost as if there's no script in the buttons at all. Discern 1007 — 9y

1 answer

Log in to vote
1
Answered by
BlackJPI 2658 Snack Break Moderation Voter Community Moderator
9 years ago

After trying to figure out what was going on here, I decided to take what you had and make it more efficient and simple. It seems that you're doing a lot of over thinking.

First off, instead of making it so that there is a script for every option, you can make it more efficient by adding all the options to an array, like so:

local options = {script.Parent.Minigame1, script.Parent.Minigame2, script.Parent.Minigame3} 

Now that you have all the options in one place, you can call one function to run all three buttons.

local options = {script.Parent.Minigame1, script.Parent.Minigame2, script.Parent.Minigame3}

for _, option in ipairs (options) do
    option.MouseButton1Click:connect(function()
        -- code
    end)
end

After all this is in place, you can really simplify the code.

local options = {script.Parent.Minigame1, script.Parent.Minigame2, script.Parent.Minigame3}
local currentVote -- Variable to hold what the players current vote is

for _, option in ipairs (options) do
    option.MouseButton1Click:connect(function()
        if not currentVote and not option.Voted.Value then -- If they have no current vote

            -- Add new vote
            option.Voted.Value = true
            game.Workspace[tostring(option) .. "Votes"].Value = game.Workspace[tostring(option) .. "Votes"].Value + 1
            option.BackgroundColor3 = Color3.new(0, 1, 0)
            currentVote = option

        elseif currentVote and not option.Voted.Value then -- If they have a current vote

            -- Remove previous vote
            currentVote.Voted.Value = false
            game.Workspace[tostring(currentVote) .. "Votes"].Value = game.Workspace[tostring(currentVote) .. "Votes"].Value - 1
            currentVote.BackgroundColor3 = Color3.new(1, 1, 1)

            -- Add new vote
            option.Voted.Value = true
            game.Workspace[tostring(option) .. "Votes"].Value = game.Workspace[tostring(option) .. "Votes"].Value + 1
            option.BackgroundColor3 = Color3.new(0, 1, 0)
            currentVote = option

        end
    end)
end

And there you have it, a more efficient and simpler function!

0
Wow, this is fantastic. Thank you so much! Discern 1007 — 9y
Ad

Answer this question