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

Vote to regen model poll won't work it's a little buggy help? (Read Desc)

My script won't work I look at roblox wiki and it still didn't work I tried working on it for an hour please help. Here's the script. Thank you for your time. Bye!

-Bobthebuildbobb

model = game.Workspace.Old
backup = model:clone()
enabled = true
function regenerate(asd)
	needed = game.Players.NumPlayers / 2 --Basicly, half the players are needed
	if script.Parent.Value == needed then --If the amount of people voted is equal to the amount of people neded
		model:remove() --Remove the model
		wait(2)--Change this number to display the gap between when the regen will start (Removing the model) and when it will take action (Replace the model)
		model = backup:clone() --Replace the model
		model.Parent = game.Workspace --Puts it in the workspace so we can see it.
		model:makeJoints() --Makes the joints so it dosen't fall apart
		p = game.Players:GetChildren() --Gets all the players
		for i=1,#p do --For every player, do this
			if p[i].className == "Player" then --If the object found is a player
				p[i].PlayerGui.voter.voted.Value = 0 --Sets the players vote value to 0 so they can vote again
				p[i].PlayerGui.voter.TextButton.Text = "Regen ("..tostring(needed).." needed)"--Sets the message back to default
			end
		end
	else -- If there's not enough players to regen
	p = game.Players:GetChildren()--Gets all the players
		for i=1,#p do --For every player, do this
			if p[i].className == "Player" and p[i].PlayerGui.voter.voted ~= 1 then --If it's a player, and this person hasen't voted
				p[i].PlayerGui.voter.TextButton.Text = "Regen ("..tostring(needed).." needed)"
			else--If the player has voted
				if needed == 1 then
					p[i].PlayerGui.voter.TextButton.Text = "Someone else has voted! "..tostring(needed).." vote needed!"
				else
					p[i].PlayerGui.voter.TextButton.Text = "Someone else has voted! "..tostring(needed).." votes needed!"
				end
			end
		end
	end
end
script.Parent.Changed:connect(regenerate

4 comments

3

If you are bob the builder can't you fix it?

0

bob the builder exposed

0

nu please i only build building not script please help

0

^^^ lmao

1 answer

If you put this script into Roblox Studio, you will get a few notices:

  • You are missing a parenthesis ) at the very end (but I'm assuming you just missed that when copying it over)
  • You have a few global variables that the Script Analysis window (see the View Menu at the top to open it) suggests making local (which you should do). Further, you can make the variables declared outside the function 'local' as well. None of this fixes your script, it's just good practice.

Also, some notes from me:

  • It's better practice to use Clone and Connect instead of the deprecated clone and connect (anything deprecated can be removed at any time) -- same for all Roblox functions that start with a lower case letter (Roblox has gradually been moving to starting with capitals for the first letter). Also applies to ClassName instead of className.
  • No need for a parameter asd in the function -- if you don't need it, just omit it.
  • The comments in your script might be helpful if you're just starting out (I assume you didn't write them), but many of them just restate in English what is obvious from the Lua code. ex model:Remove() --Remove the model) -- this sort of comment is useless outside of learning purposes.

More problems / potential problems:

  • Line 22 you have voter.voted but based on line 15 it should be voter.voted.Value
  • Line 6 it should be >= not == -- if you have more people than you need, surely you still want to regenerate the model?
  • Line 7, :Remove is deprecated - you're encouraged to use :Destroy() instead (though they do slightly different things, but I think Destroy is what you want here anyway)
  • Line 16 and related lines in the second half of the script are attempting to modify a player's GUI (which only exists on that player's client) in a script that needs to be on the server -- you have to split this up
  • Based on script.Parent.Changed, I assume this script is supposed to be a child of an IntValue - is it?
  • You don't have any sort of debounce (maybe that's what enabled was supposed to do?), meaning regenerate could activate multiple times simultaneously. ex, say there are 10 players and players 1-4 have already voted. Now... ** Say player 5 votes. regenerate will be called, it'll determine that enough votes have been acquired, and it will destroy the model and call wait(2). ** Now say player 6 also votes at the same time that player 5 does. Thus, regenerate will be called again (while the first call is on the wait(2) command) and repeat the same steps! (Each call is on a separate thread or coroutine. 2 threads never run at exactly the same time, but they take turns.) ** After 2 seconds, each thread will take turns executing the rest of the script - most of that isn't a big deal, but the :Clone() and :MakeJoints() and everything else is wasteful.

You didn't specify whether there was any Output (the Output window can also be opened from the View menu, if it isn't already). What exactly is output (if anything) and when are important details for a question like yours.

Now, to fix the client/server problem, you should read and understand https://scriptinghelpers.org/guides/server-client-relationship-in-roblox

Specifically, you'll need to rewrite all the scripts involved to use RemoteEvents (see the Roblox tutorial on them). The server script becomes:

local model = game.Workspace.Old
local backup = model:Clone()
local regenerating = false -- whether regeneration is in progress
local event = Instance.new("RemoteEvent")
event.Name = "VoteRegenerate"
event.Parent = game.ReplicatedStorage
local voted = {} -- Dictionary<player, true if voted to regenerate, nil otherwise>
local function getNumVoted() -- get the number of players who voted to regenerate
    local num = 0
    for p, _ in pairs(voted) do
        num = num + 1
    end
    return num
end
local function getTotalVotesNeeded()
    return game.Players.NumPlayers / 2
end
local function updateClients()
    event:FireAllClients(getNumVoted(), getTotalVotesNeeded()) -- TODO clients should use these two numbers to determine what to display. If the first number is 0, that also means that the player's vote has been reset.
end
local function regenerate()
    if regenerating then return end -- A different thread is already regenerating
    regenerating = true -- Prevent another thread from regenerating until we're done on this thread
    model:Destroy()
    wait(2) --Change this number to display the gap between when the regen will start (Removing the model) and when it will take action (Replace the model)
    model = backup:Clone() --Replace the model
    model.Parent = game.Workspace --Puts it in the workspace so we can see it.
    model:MakeJoints() --Makes the joints so it doesn't fall apart
    regenerating = false
end
event.OnServerEvent:Connect(function(player, vote)
    vote = vote and true or nil -- this "and true or nil" guarantees that 'vote' is either true or nil, even if it starts out as false or any other value (which we don't want to store)
    if voted[player] == vote then return end -- player isn't changing their vote so don't do anything
    voted[player] = vote
    local numVoted = getNumVoted()
    local totalVotesNeeded = getTotalVotesNeeded()
    if numVoted >= totalVotesNeeded then
        regenerate()
        voted = {} -- resets all votes
        updateClients()
    else
        updateClients()
    end
end)

I don't have time to write the client script for you; it has to work with the event defined in the server script and update the player's GUI. The script above makes it so that you don't need that IntValue in the workspace.

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