Help with RemoteEvent

So I have a local script within a tool that will fire an event

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage.Events:WaitForChild("AddExp")

tool.Equipped:Connect(function(mouse)
	mouse.Button1Down:Connect(function()
		if mouse.Target and mouse.Target.Parent then
			spawn(function()
				Attack_2 = true
				Attack_2 = Remotes:WaitForChild("BARRAGE"):InvokeServer(tool,AnimationsFolder)
				remoteEvent:FireServer()
			end)
		end
	end)
end)

I also have a script that will add EXP once that event is fired

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage.Events:WaitForChild("AddExp")
local player
local tool = script.Parent
    repeat wait()
player = tool.Parent.Parent
until tool.Parent.Name == "Backpack"
local leveling_data = player:WaitForChild("LevelingData")
local exp = leveling_data:WaitForChild("EXP")
local function GiveExp()
    exp.Value = exp.Value + 100
end

remoteEvent.OnServerEvent:Connect(GiveExp)

But the script doesn’t give EXP and it shows no error in the output. What can I try to fix this?

Can you please show the error message. I can’t run code in my studio to get it bcs it’s not possible due to idk what do you mean by some of values (like Remotes, Attack_2 and etc.). Thanks.

Like I said, it doesn’t print out any error message, it just doesn’t work.

Ohhh, I’m sorry, I dismissed “no”. Check all possible IFs, WHILEs and other stuff that can actually don’t let the program work. Also, check object to WaitForChild actually exists and appears to make the code proceed to the next lines

Is this a Script inside of a Tool? Scripts don’t run inside of descendants of the Player, it need to be a LocalScript for that. Move this elsewhere and use the player argument from the OnServerEvent to find the player and add experience.

Actually, this is false. I ran simple script in player’s Backpack and it works. You might to be wrong with localscripts should be runned there

I have a local script in the tool that will fire a remote event and a script in ServerScriptStorage will give the player EXP when the remote event is fired.

I’m pretty sure you’d need to define the player with your OnServerEvent function if you have it there? Cause on the LocalScript it looks like the event is firing, it just doesn’t know who exactly is firing it so it’s probably returning back as nil or something

1 Like

How should I do that? It seems complicated to me.

B r u h

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage.Events:WaitForChild("AddExp")

local function GiveExp(player)
    local leveling_data = player:WaitForChild("LevelingData")
    local exp = leveling_data:WaitForChild("EXP")
    exp.Value = exp.Value + 100
end

remoteEvent.OnServerEvent:Connect(GiveExp)

All you’re really doing is just adding the player argument in the first parameter of your local function so that it knows who fired it, also please don’t use repeat wait loops, it’s not entirely the best way to check for an argumemt

first of all, your getting the players character, also, to get the player, a remote event already sends the player, so you can do this to get the player:

remoteEvent.OnServerEvent:Connect(function(player)
     -- do your stuff
end)

doing that should fix the error

Also, local scripts can NOT run in the workspace, when a player equips it, it is in the workspace.

Didn’t work, anything else I can try?

You could probably just remove the local function & just replace it with this I guess:

remoteEvent.OnServerEvent:Connect(function(player)

It should work? If not, then 1 of your variables are probably not replicated to the server to be visible

Didn’t work, also what do you mean one of my variables aren’t visible for the server?

They aren’t replicated (Or the same), as both the server & client are 2 different sides, it also could be that you need to put it in ServerScriptService instead? Try debugging it with print statements

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Player = game.Players.LocalPlayer
local mouse = Player:GetMouse()
local remoteEvent = ReplicatedStorage.Events:WaitForChild("AddExp")

tool.Equipped:Connect(function()
	mouse.Button1Down:Connect(function()
		if mouse.Target and mouse.Target.Parent then
			spawn(function()
				Attack_2 = true
				Attack_2 = Remotes:WaitForChild("BARRAGE"):InvokeServer(tool,AnimationsFolder)
				remoteEvent:FireServer()
			end)
		end
	end)
end)

Try this, instead of getting the mouse through the function, I just added it at the top.

You’re correct. It’s probably a bad practice though. I could have sworn that Scripts have had issues interacting with local things like PlayerGui, and I can’t imagine that this would be different. I might be misremembering something.

you cannot see mouse events on the server side, so instead fire the event in the local script when the mouse is clicked.