Issue with remote functions

I’m working on improving my skills with remote functions, trying to make sure I’m not confused with them in the future. The problem is that my scripts aren’t working with nothing in the output, I think this is a result of my local script thats invoking the server is within the part. I don’t know how else to reference to the part though.

My leaderstats script:

game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder", plr)
	folder.Name = "leaderstats"
	
	local money = Instance.new("IntValue", folder)
	money.Name = "Money"
end)

My local script within the part:

local part = script.Parent
local value = part.Value
local money = game.Players.LocalPlayer.leaderstats.Money
part.Touched:Connect(function()
	local x = game.ReplicatedStorage.RemoteFunction:InvokeServer(value)
	money.Value = money.Value + x
end)

The events I’m returning on the server:

game.ReplicatedStorage.RemoteFunction.OnServerInvoke = function(player, value)
	print(player)
	if player.leaderstats.Money.Value >= value.Value then
		return player.leaderstats.Money.Value - value.Value
	elseif player.leaderstats.Money.Value < value.Value then
		return 0
	end
end

A picture of my workspace as well:

1 Like

This isn’t practical use for RemoteFunctions. Use RemoteEvents for that.

2 Likes

Don’t I need to check if the player has the right amount and return it?

1 Like

Oh, sorry, didn’t see the variable stored from invoking it. Could you fix your formatting for the post real quick? I’m trying to understand the issue

There we go, I got it work, I messed it up a little.

I see the issue, you don’t seem to Parent anything in the leaderstats script.

I parented the folder with the plr, and the money with the folder.

Yeah nevermind I began going blind. So the issue is when you touch the part, nothing at all happens (UI,value)?

Nothing happens, even in the output, so I think it has to do with the local script in part. I put a print below the touch event and nothing happened when I touched the part.

Nooow you cleared it up. LocalScripts don’t run in Workspace.

Oh, how am I gonna run this or make this touched event happen?

You can parent it under StarterGui/Pack or any of the StarterPlayer folder.

1 Like

But again, this code isn’t practical. Just do the check on the server using a server side .Touched and then send a RemoteEvent to update the text.

1 Like

I can reference the part from a local script in startergui/starterplayer folder?

Yeah, you’d do workspace.Part

Output said part is not a member of workspace, local scripts can’t reference parts.

Local scripts can reference parts. You just have to target your part using the path. You really want it this way?

I’m just practicing remote functions, what do you mean by path though?

Ok, just replace workspace.Part to workspace:WaitForChild("Part").

1 Like

don’t use local scripts when setting a value in leaderstats! it wont show to other players and it wont save

use RemoteEvents instead to set the value in the server

also, any local scripts under Workspace (except local scripts under Models like a player’s character) won’t run. instead, create a normal script then set the RunContext to Client so it will ‘turn into a local script’ because normal/server scripts can run under Workspace

P.S. you can get the player that touched the part using the otherPart parameter which gives the limb of the player that touched the part. you can use that limb to get the player using Players:GetPlayerFromCharacter(otherPart.Parent)

1 Like