How to wait until a player clicks to continue a server script?

How would I make this server script wait until the player clicks after chatting to do a function?

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if Message == "Flame!" or "flame!" or "Flame" or "flame" then
			local player = game.Players:WaitForChild(Player.Name)
			local m = player:GetMouse()
			m.Button1Down:Connect(function()
				print("Clicked!")
			end)
		end
	end)
end)
1 Like

This errors as:
ServerScriptService.SpellManager:6: attempt to index nil with ‘Button1Down’

Is this a LocalScript or a script?

It is a script in serverscriptservice

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if Message == "Flame!" or Message == "flame!" or Message == "Flame" or Message == "flame" then
			local m = Player:GetMouse()
			m.Button1Down:Connect(function()
				print("Clicked!")
			end)
		end
	end)
end)

Local script, place it inside StarterPlayerScripts folder.

That could be your problem right there. Try moving this to a LocalScript in StarterGui and see what happens. Just remove the first function and add a local player function. Try this and see if it works:

local Player = game.Players.LocalPlayer
Player.Chatted:Connect(function(Message)
		if string.lower(Message) == "flame!" or string.lower(Message) == "flame" then
			local player = game.Players:WaitForChild(Player.Name)
			local m = player:GetMouse()
			m.Button1Down:Connect(function()
				print("Clicked!")
			end)
		end
	end)

Check my solution above, you already have the “Player” instance defined so there’s no need to redefine them.

Couple problems here:

  • Can’t use .Button1Down on the server under a UIObject
  • In your Player.Chatted if statement, you’d have to change the if statement to:
if string.lower(Message) == "flame!" or string.lower(Message) == "flame" then

This works but how would I make it only work once per chat

This is attempting to get the Mouse instance from the player’s name, :GetMouse() is a function of the player instance.

Did Roblox change the way if statements work? O_O Does that if statement really work?

Ref:

if Message == "Flame!" or "flame!" or "Flame" or "flame" then

I think they had the wrong code copied & pasted or they quoted the wrong person.

Yes it works. And it is a lot cleaner.

1 Like

Add a debounce. Here’s the script:

local Player = game.Players.LocalPlayer
local Used = false
local Mouse = Player:GetMouse()
Player.Chatted:Connect(function(Message)
	if string.lower(Message) == "flame!" or string.lower(Message) == "flame!" then
           Used = true
    end
end)

Mouse.Button1Down:Connect(function()
    if Used == true then 
		print("Clicked!")
        Used = false
    end
end)

This still won’t work, you can’t check multiple conditions like that.

local debounce = false

game.Players.PlayerAdded:Connect(function(Player)
	Player.Chatted:Connect(function(Message)
		if Message == "Flame!" or Message == "flame!" or Message == "Flame" or Message == "flame" then
			local m = Player:GetMouse()
			m.Button1Down:Connect(function()
				if not debounce then
					print("Clicked!")
					debounce = true
				end
			end)
		end
	end)
end)

This will make it so that the print(“Clicked!”) command will only execute once. Even if someone chats “flame” or “flame!” etc.

1 Like

ah my bad, right. (extra text ignore this)

Alright, try this (after editing it a few times I think this should work)

local Player = game.Players.LocalPlayer
local Used = false
local Mouse = Player:GetMouse()
Player.Chatted:Connect(function(Message)
	if string.lower(Message) == "flame!" or string.lower(Message) == "flame!" then
       Used = true
    end
end)

Mouse.Button1Down:Connect(function()
    if Used == true then 
		print("Clicked!")
        Used = false
    end
end)

Thought you should know, this is only useful and mostly used in a server script as local scripts are able to use the LocalPlayer instead

I’m well aware, I’m not going to make any unnecessary changes though.

Wrong, player is referring to the player, he is just using Player.Name for WaitForChild, which returns the child and NOT its name.

You’ll use the disconnect function, but you have to disconnect a connection (obviously), so it would look something like this:

local Player = game.Players.LocalPlayer
local function Chatted(Message, connection)
   if Message == "Flame!" or "flame!" or "Flame" or "flame" then
		local m = Player:GetMouse()
		m.Button1Down:Connect(function()
			print("Clicked!")
		end)
      connection:Disconnect()
   end
local connection = Player.Chatted:Connect(Chatted(message, connection)

You might have to modify this though

1 Like