Why isn't my remote function working as I intend it to?

Here is an example code on how to use RemoteFunction

Client

local Text = RemoteFunction:InvokeServer()
print(Text)

Server

RemoteFunction.OnServerInvoke = function(Player)
	local Text = "Hello There!"
	return Player.Name.. " said: ".. Text
end

You didn’t assigned the variable when calling `InvokeServer.

--Change this
debounceFunction:InvokeServer(AzureOre, true)
--To this
local result = debounceFunction:InvokeServer(AzureOre, true)
print(result)

Also, i’m not sure if arguments are passed while doing this

debounceFunction.OnServerInvoke = OreTransparency

So you might have to change it to this

debounceFunction.OnServerInvoke = function(Player, Ore, localDebounce)
    OreTransparency(Player, Ore, localDebounce)
end
2 Likes

Oddly enough, it doesn’t print the result for some reason. I’ve changed the script to these changes, but nothing happens…

Serverscript

local function OreTransparency(player, Ore, localDebounce)
	print("test")
	local char = player.Character
	local hum = char:FindFirstChildOfClass("Humanoid")
	if not debounce then
		if hum then
			local pickaxe = char:FindFirstChild("Pickaxe")
			if pickaxe then
				for _, v  in(Ore:GetChildren()) do
					if v:IsA("BasePart") and v.Name ~= "Hitbox" then
						v.Transparency += 0.25
						if v.Transparency == 1 then
							if Ore.Name == "Rock" then
								print("rock!")
							elseif Ore.Name == "IronOre" then
								print("iron!")
							end
						end
					end
				end
				debounce = true
				task.wait(3)
				debounce = false
			end
		end
	end
	if localDebounce == true then
		localDebounce = false
		return localDebounce
	end
end

debounceFunction.OnServerInvoke = function(player, Ore, localDebounce)
	OreTransparency(player, Ore, localDebounce)
end

Localscript

local AzureOre = script.Parent
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Hitbox = script.Parent:WaitForChild("Hitbox")
local replicatedStorage = game:GetService("ReplicatedStorage")
local debounceFunction = replicatedStorage.DebounceFunction
local Character = Player.Character or Player.CharacterAdded:Wait()
local Pickaxe = Character:WaitForChild("Pickaxe")
local Handle = Pickaxe:WaitForChild("Handle")
local Count = 0
local Active = false
local debounce = false

Hitbox.Touched:Connect(function(player)
	local function Check()
		if Handle:GetAttribute("IsActivated") == true and not debounce then
			debounce = true
			Count += 1
			local Cast = workspace:Raycast(Handle.Position, Handle.CFrame.LookVector * 2)
			if Cast then
				if Cast.Instance.Name ~= AzureOre.Name then return end
			else
				return
			end
		end
	end

	repeat task.wait(1) Check() until Count == 5
	local result = debounceFunction:InvokeServer(AzureOre, true)
	print(result)
	Count = 0
end)

Also, just noticed that your local script get stuck to the repeat until

repeat task.wait(1) Check() until Count == 5

Because debounce is on false, then get changed to true at the first count and never go to false again so your count can’t go to 5.

1 Like

I’m trying to find a way to communicate from the server to the client, to change the debounce value to false through the localdebounce variable on the server script

1 Like

However, my attempts have not yielded anything…

1 Like

I’ve been trying to follow what’s been linked here, but it seems rather difficult to achieve with my current knowledge on remote functions.

1 Like

Oh alright, so you most likely need to do this

repeat task.wait(1)
    Check()
    local result = debounceFunction:InvokeServer(AzureOre, true)
    if result then
        debounce = result
    end
until Count == 5
1 Like

I’ve done this, but now the serverscript seems to be on an infinite loop upon the first click with the pickaxe tool. Now, the ore disappears regardless of if I click it once with the pickaxe. I’ve put a print statement below the OreTransparency function to show this off.

image

1 Like

Here’s the server script, to see if the loop can be caught.

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local debounceFunction = ReplicatedStorage.DebounceFunction
local OreTransparency = 0
local debounce = false
local debounce2 = false

local function OreTransparency(player, Ore, localDebounce)
	print("test")
	local char = player.Character
	local hum = char:FindFirstChildOfClass("Humanoid")
	if not debounce then
		if hum then
			local pickaxe = char:FindFirstChild("Pickaxe")
			if pickaxe then
				for _, v  in(Ore:GetChildren()) do
					if v:IsA("BasePart") and v.Name ~= "Hitbox" then
						v.Transparency += 0.25
						if v.Transparency == 1 then
							if Ore.Name == "Rock" then
								print("rock!")
							elseif Ore.Name == "IronOre" then
								print("iron!")
							end
						end
					end
				end
				debounce = true
				task.wait(3)
				debounce = false
			end
		end
	end
	if localDebounce == true then
		localDebounce = false
		return localDebounce
	end
end

debounceFunction.OnServerInvoke = function(player, Ore, localDebounce)
	OreTransparency(player, Ore, localDebounce)
end
1 Like

Despite the modifications, the ore still prints 15 times, but now the script is on a loop…
image

1 Like

Your script is on a loop because you’re not resetting the Count variable to 0 here.

1 Like

There is no need to reset count to 0, it is supposed to reach 5 which then should end the loop, but here it isn’t reaching 5 probably because of something wrong into the server script but i’m lazy to review the entire code so i let someone else trying to help him.

1 Like

If the Count doesn’t get reset, and the Hitbox is touched again then the repeat until loop will never equal 5 and will run forever.

1 Like

Nah, if the Hitbox is touched again then a new repeat loop will start using the current count value, and both loop will end when the count reach 5… you need to reset the count only when it reached 5, after the loop end.

Not sure, but i think that the repeat loop yield the code so touching the hitbox again could do nothing.

1 Like

Yes, a new repeat loop will start with the current Count value which is 5. It will then be 6, 7… so on and run forever

1 Like

No, if you reset it once it reached 5 it shouldn’t go above it, but anyway yeah it still is an infinite loop as the process will run over and over again

1 Like

Here you put the RemoteFunction inside the loop but previously it was outside of it, this could also be causing the looping error

1 Like

There is no looping error, this is what the OP is attempting to do.

Inside the loop, the Check function is running and is setting the debounce value to true, then the RemoteFunction is fired, something is made in server side and it return false to the client so you can set the debounce value back to false to allow the Check function to run again… it is the current problem, i’m pretty sure that something in server side is wrong which cause it to never return false to the client and getting stuck at count 1 with the debounce value to true

1 Like

What I’m trying to do is have a localscript which receives information from the server upon finishing up the transparency function, so that the debounce can be set to true when the function hasn’t started, and set to false whenever the transparency function concludes. As I wish to avoid the transparency function firing 15 times, and instead, only once.

1 Like

I do set it back to 0 once the count has reached 5.

	repeat task.wait(1) 
		Check() 
		local result = debounceFunction:InvokeServer(AzureOre, true)
		if result then
			debounce = result
		end
	until Count == 5
	Count = 0
end)
1 Like