Issue with remote functions

Should I use scripts with the on server event to change values?

I need the server to check if the player has the right value for the thing he/she’s buying.

you can do that directly when touching the part. you don’t need a RemoteFunction or RemoteEvent for this.

if you dont get what I’m trying to say, you can follow these steps:

  1. Delete the LocalScript in part
  2. Delete the RemoteFunction script in ServerScriptService
  3. Delete the RemoteFunction in ReplicatedStorage
  4. Insert a Normal Script in the part
  5. Paste this code (the notes explain each line)
local Players = game:GetService("Players") -- the Players service which is the container of all Player objects

local Part = script.Parent -- the part
local Value = Part.Value -- the Value inside the part

local function partTouched(otherPart: BasePart) -- this function will run if something touched the part
	local Character = otherPart.Parent -- the parent of the part that touched this part
	
	if Character.ClassName == "Model" then -- if Character is a model (because a player's character is always a Model)
		local Player = Players:GetPlayerFromCharacter(Character) -- gets the player that has the Model as the character
		
		if Player ~= nil then -- checks if the part was touched by a player; checks if Players:GetPlayerFromCharacter(Character) returned nil (nothing) or not
			-- it returned something, therefore Character is a character of a player in game
			local leaderstats = Player:FindFirstChild("leaderstats") -- gets the leaderstats folder of the player
			
			if leaderstats ~= nil then -- checks if the player has leaderstats
				-- player has leaderstats
				local Money = leaderstats:FindFirstChild("Money")
				
				if Money ~= nil then
					if Money.Value >= Value.Value then -- if the player's money is enough to buy it or not
						-- if yes
						Money.Value -= Value.Value -- decreases the player's money; equal to `Money.Value = Money.Value - Value.Value`
					else
						-- if not
						Money.Value = 0
					end
				end
			end
		end
	end
end

Part.Touched:Connect(partTouched) -- makes it that partTouched will run if a player touched the part; connects the partTouched function to the Part.Touched event that fires/runs when something
  1. You can remove the notes if you want (as long as you understood the script)

i hope this helps

1 Like

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