Remote event wont fire?

Hello, I’m making a tool with a Gui and remote events. However, pressing F works fine but clicking the Gui doesn’t work even though they pretty much have the same code.

local uis = game:GetService("UserInputService")

local Cloud = script.Parent.CloudButton
local Lightning = script.Parent.LightningButton
local RockSkin = script.Parent.RockSkinButton
local Wave = script.Parent.WaveButton

local AttackEvent = script.Parent.Attack
local ToggleEvent = script.Parent.Toggle

uis.InputBegan:Connect(function(Input, IsTyping)
	if IsTyping then return end

	if Input.KeyCode == Enum.KeyCode.E then
		AttackEvent:FireServer("Lightning")
		
	elseif Input.KeyCode == Enum.KeyCode.Q then
		AttackEvent:FireServer("Wave")
		
	elseif Input.KeyCode == Enum.KeyCode.R then
		ToggleEvent:FireServer("RockSkin")
		
	elseif Input.KeyCode == Enum.KeyCode.F then
		ToggleEvent:FireServer("Cloud")
	end
end)

-- mobile support

function clicked(whichButton)
	if whichButton == Lightning then
		AttackEvent:FireServer("Lightning")
	elseif whichButton == Wave then
		
		AttackEvent:FireServer("Wave")
	elseif whichButton == RockSkin then
		
		ToggleEvent:FireServer("RockSkin")
	elseif whichButton == Cloud then
		
		print("clicked")
		ToggleEvent:FireServer("Cloud")
	end
end

for _, button in ipairs({Lightning, Wave, RockSkin, Cloud}) do
	print(button)

	button.MouseButton1Click:Connect(function()
		clicked(button)
	end)
end
'''
1 Like

Can you post the server code that handles this event, as well as where that server script is stored?

I don’t know what you mean by “Can you post the server code that handles this event” but here is the server sided code (note its very messy because it’s my old code and I’m fixing it)

local remoteEvent = script.Parent.GUI.Magic.Frame.Toggle
local rockpartical = script.Parent.Handle.Spawn.rocks
local Cloud
local CloudMesh
local MoveCode
local partical
local character
local WalkSpeed
local JumpPower
local Anim
local track

script.Parent.Equipped:Connect(function()
	character = script.Parent.Parent
end)

remoteEvent.OnServerEvent:Connect(function(plr,event,msg)
	if event == "Cloud" then
		if character.Humanoid.Health > 1 then
			
			Anim = Instance.new("Animation")
			Anim.AnimationId = "http://www.roblox.com/Asset?ID=16234647732"
			track = script.Parent.Parent.Humanoid:LoadAnimation(Anim)
			track.Priority = Enum.AnimationPriority.Action2

			WalkSpeed = character.Humanoid.WalkSpeed
			JumpPower = character.Humanoid.JumpPower
			
			character.Humanoid.JumpPower = 0
			character.Humanoid.WalkSpeed = 0
			
			track:Play()
			Anim:Destroy()

		end
	end
end)
1 Like

There’s an issue with the MouseClickedEvent, as testing the code on my side it works fine

1 Like

I’ve never seen anyone do a for loop like this. Does anything change if you do:
for _, button in script.Parent:GetChildren() do
if not Button:IsA(“TextButton”) then continue end

– clicking logic
end

1 Like

A loop like this is usually done like so:

for index, value in pairs(Table) do
	print("This is the index of the value in the table: "..index)
	print("This is the value: "..value)
end

And my testing concludes that this should work but something is wrong with the MouseClickedEvent

1 Like

I know, I’ve just never seen someone do it the exact way OP did where they created the table inside the loop haha

1 Like

It’s just a way of limiting the amount of things it loops through to optimize code

--Goes through all children
for index, value in pairs(Object:GetChildren()) do
	print("This is the index of the value in the table: "..index)
	print("This is the value: "..value)
end
--Goes through specific objects in the children rather than all children
for index, value in pairs({Object, Object, Object, Object}) do
	print("This is the index of the value in the table: "..index)
	print("This is the value: "..value)
end
2 Likes

Oh, what should I do instead? I’m not a very good scripter yet so I need some help here.

1 Like

There’s nothing wrong with your code, but the objects you are defining (Cloud, Lightning, RockSkin, Wave) have to be either TextButtons or ImageButtons for the MouseClicked event to be applicable for them

1 Like

It is a text button. I’m very confused

1 Like

I believe the issue is ipairs, unfortunately the documentation on it is very little and doesn’t explain it well, so I don’t know what about ipairs makes it unreliable here, so just use normal pairs instead
Try this:

local uis = game:GetService("UserInputService")

local AttackEvent = script.Parent.Attack
local ToggleEvent = script.Parent.Toggle

local Buttons = {
	Cloud = {
		Object = script.Parent.CloudButton;
		Event = AttackEvent;
	};
	Lightning = {
		Object = script.Parent.LightningButton;
		Event = AttackEvent;
	};
	RockSkin= {
		Object = script.Parent.RockSkinButton;
		Event = ToggleEvent;
	};
	Wave = {
		Object = script.Parent.WaveButton;
		Event = ToggleEvent;
	};
}
--[[
Alternate version of the above table
local Buttons = {
	Cloud = {Object = script.Parent.CloudButton, Event = AttackEvent};
	Lightning = {Object = script.Parent.LightningButton, Event = AttackEvent};
	RockSkin= {Object = script.Parent.RockSkinButton, Event = ToggleEvent};
	Wave = {Object = script.Parent.WaveButton, Event = ToggleEvent;};
}
]]

uis.InputBegan:Connect(function(Input, IsTyping)
	if IsTyping then return end

	if Input.KeyCode == Enum.KeyCode.E then
		AttackEvent:FireServer("Lightning")
		
	elseif Input.KeyCode == Enum.KeyCode.Q then
		AttackEvent:FireServer("Wave")
		
	elseif Input.KeyCode == Enum.KeyCode.R then
		ToggleEvent:FireServer("RockSkin")
		
	elseif Input.KeyCode == Enum.KeyCode.F then
		ToggleEvent:FireServer("Cloud")
	end
end)

--Loop through the Buttons table (there's no reason to use ipairs instead of pairs here)
for name, button in pairs(Buttons) do
	print(button)

	button.Object.MouseButton1Click:Connect(function()
		button.Event:FireServer(name)
	end)
end
'''
1 Like

doesn’t work, I can give you the model if that helps

1 Like

Yes that would be very helpful

1 Like

Alright, the id is 16236928781. You can ignore the disabled scripts because those are outdated

He i might found your problem this : uis.InputBegan:Connect(function(Input, IsTyping)
if IsTyping then return end
there you return to the end of the funktion so no event gets trigert because you need a IsTyping == false there

Hope that helps

The problem was you were cloning the Gui so the click script would fire a cloned event rather than the actual event

I’ve went ahead and fixed the problem along with a few optimizations and comments; if you have any further questions don’t be afraid to ask!
Magic Spell Book.rbxm (26.3 KB)

Also I’d recommend using adonis admin rather than kohls

PS: I have no clue why the UserInputService worked fine but the MouseClicked didn’t

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.