Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why is this script not making PlayerVotes stop at one?

I am trying to create a voting system, and this script adds the votes to the vote counter above it. However, even though I have variables that keep track of the player's votes, the voting per player does not stop at one. Here is my script. Note my voting system is not complete yet.

local VotingSystem = script.Parent.Parent.Parent
local pad = script.Parent
local Debounce = 1
local PlayerAlreadyVoted = false
local PlayerVotes = 0
print("GTF1")
Votes = 0
print("GTF2")

function OnTouch(Toucher)
	print("GTF3")
	if Toucher.Parent:FindFirstChild("Humanoid") then
		print("GTF4")
		Votes = Votes + 1
		wait(Debounce)
		print("GTF5")
		print(Votes)
		print("GTF6")
		PlayerVotes = 1
		if PlayerVotes == 1 then
			print("Player already voted!")
			script.Parent.Parent.Parent.VoteDisplay1.SurfaceGui.TextLabel.Text = Votes
		print("GTF7")
		end
	print("GTF9")
	end
print("GTF10")
end


pad.Touched:Connect(OnTouch)

2 comments

0

You aren't actually stopping the script from adding more votes on. So every time it runs, it will add 1 because there is no 'if' statement to stop it from doing so

0

Essentially, all you need to do is check if they already voted and if they did return nil this just breaks the function from happening.

2 answers

##Explanation

Yes, they don't. It just executes the other lines if PlayerVotes is 1. If they are more than 1, why not return? Also, keep your functions local, and basically what the script does is that ifPlayerVotes are 2 or greater,it does nothing! You never checked if PlayerVotes is greater than 1 though.

local VotingSystem = script.Parent.Parent.Parent
local pad = script.Parent
local Debounce = 1
local PlayerAlreadyVoted = false
local PlayerVotes = 0
print("GTF1")
local Votes = 0
print("GTF2")


local function OnTouch(Toucher)
    if PlayerVotes > 1 then
         return
      end
    print("GTF3")
    if Toucher.Parent:FindFirstChild("Humanoid") then
        print("GTF4")
        Votes = Votes + 1
        wait(Debounce)
        print("GTF5")
        print(Votes)
        print("GTF6")
        PlayerVotes = 1
        if PlayerVotes == 1 then
            print("Player already voted!")
            script.Parent.Parent.Parent.VoteDisplay1.SurfaceGui.TextLabel.Text = Votes
        print("GTF7")
        end
    print("GTF9")
    end
print("GTF10")
end


pad.Touched:Connect(OnTouch)

You need some sort of statement that stops the character from voting if they have already done so.

local VotingSystem = script.Parent.Parent.Parent
local pad = script.Parent
local Debounce = 1
local PlayerAlreadyVoted = false
local PlayerVotes = 0
print("GTF1")
Votes = 0
print("GTF2")

function OnTouch(Toucher)
    print("GTF3")
    if PlayerVotes == 1 then return end
   	 if Toucher.Parent:FindFirstChild("Humanoid") then
       	 print("GTF4")
         Votes = Votes + 1
         wait(Debounce)
         print("GTF5")
         print(Votes)
         print("GTF6")
         PlayerVotes = 1
         if PlayerVotes == 1 then
            print("Player already voted!")
            script.Parent.Parent.Parent.VoteDisplay1.SurfaceGui.TextLabel.Text = Votes
        print("GTF7")
        end
    print("GTF9")
    end
print("GTF10")
end


pad.Touched:Connect(OnTouch)

4 comments

0

Basically, you sort of copied my script.. -_-

0

Okay, kid. I literally submitted while you were typing. I hadn't seen yours until I did mine. You need to chill out

0

Oh oh oh! Look who changed their script to look even more like mine... how the turns tabled.

0

Lmao It’s the answer of course it will look like yours...

This discussion is preserved as it was. New answers, comments, and votes are closed.