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
local event = Instance.new("RemoteEvent")
event.Name = "VoteRegenerate"
event.Parent = game.ReplicatedStorage
local voted = {}
local function getNumVoted()
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())
end
local function regenerate()
if regenerating then return end
regenerating = true
model:Destroy()
wait(2)
model = backup:Clone()
model.Parent = game.Workspace
model:MakeJoints()
regenerating = false
end
event.OnServerEvent:Connect(function(player, vote)
vote = vote and true or nil
if voted[player] == vote then return end
voted[player] = vote
local numVoted = getNumVoted()
local totalVotesNeeded = getTotalVotesNeeded()
if numVoted >= totalVotesNeeded then
regenerate()
voted = {}
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.
4 comments
If you are bob the builder can't you fix it?
bob the builder exposed
nu please i only build building not script please help
^^^ lmao