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

Help Someone? Elseif error with script for Don't Press the Button

I have tried everything to fix this I don't know how to though Please help

``script.Parent.Parent.Parent.Parent.Parent:Connect(function(click)

local event = math.random(1, 3)
print(event)

if event == 1 then
	local Obby = game.ServerStorage.Obby:Clone()
	Obby.Parent = workspace
	Obby.delete.Disabled = false
           end
     end)

   elseif event = 2 then
local LavaSpinner = game.ServerStorage.LavaSpinner:Clone()
LavaSpinner.Parent = workspace
LavaSpinner.delete.Disabled = false
 end end``

2 comments

0

What event are you connecting to the function?

0

No event. just making it so you can click it

1 answer

✓ Accepted answer

As implied with echobloxia's comment, there is no event connected to the function, meaning there's no listeners for a click. I can't tell if this is a GUI button or a ClickDetector object that is a child of a part, and even though the process is practically the same, I'll include both ways to keep it as unambiguous as possible.

--- assuming this script is referencing a click detector that's attached to a part

local clickEvent = script.Parent.Parent.Parent.Parent.Parent

clickEvent.MouseButton1Click:Connect(function()
	local event = math.random(1, 3)
	print(event)

	if event == 1 then
		local Obby = game.ServerStorage.Obby:Clone()
		Obby.Parent = workspace
		Obby.delete.Disabled = false
	elseif event == 2 then -- make sure you're using two equal signs instead of one when working with logic
		local LavaSpinner = game.ServerStorage.LavaSpinner:Clone()
		LavaSpinner.Parent = workspace
		LavaSpinner.delete.Disabled = false	
	end
end)

This next script assumes your original script is placed inside of a clickable gui button

--- assuming this script is inside of a gui button (this most likely would be a local script)
-- if you dont already know, local scripts cannot create/replicate/clone local instances to the server
-- to achieve the wanted outcome, ideally you want to use the local script to send a signal to a remote event, or better put, "communicate" with the server that you want to do something that is supposed to be replicated to the server, where everyone can see
-- you do this by using a local script to send the signal to the remote event, then use a server script to handle the request

-- and again this is purely assuming you're using a gui text button

-- local script

local replicatedStorage = game:GetService("ReplicatedStorage") -- reference the replicatedstorage as this is where your remote event will be
local event = replicatedStorage:WaitForChild("RemoteEvent") -- reference your remote event, if you dont have one, right click replicated storage -> insert object -> remote event (you probably will have to search for it or scroll until you find it)
local button = script.Parent.Parent.Parent.Parent.Parent

button.MouseButton1Click:Connect(function()
	local event = math.random(1, 3)
	print(event)
	event:FireServer(event) -- fire the server and pass through your event variable
end)

-- server script (this is just a regular script)
-- put this in server script service

local replicatedStorage = game:GetService("ReplicatedStorage") -- get the service again
local event = replicatedStorage:WaitForChild("RemoteEvent") -- reference the remote event again

event.OnServerEvent:Connect(function(plr, event) -- remote events have 1 required parameter, and it's 100% of the time the first parameter, regardless of whether or not you passed anything through, the first parameter is the player who called the event, so always add a reference to the player such as plr or player, or whatever suits your fancy
	if event == 1 then
		local Obby = game.ServerStorage.Obby:Clone()
		Obby.Parent = workspace
		Obby.delete.Disabled = false
	elseif event == 2 then -- make sure you're using two equal signs instead of one when working with logic
		local LavaSpinner = game.ServerStorage.LavaSpinner:Clone()
		LavaSpinner.Parent = workspace
		LavaSpinner.delete.Disabled = false	
	end
end)

Feel free to discard comments

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