RemoteEvent not firing? (Solved)

My upgrade system is not working, please tell me what is wrong with my code.

It basically does not fire the remote event when the button is clicked.

LocalScript that fires the event
local cash = game.Players.LocalPlayer.leaderstats.Cash
local price1 = 150
local level = game.Players.LocalPlayer.upgradestats.Upgrade
local player = game.Players.LocalPlayer

local price = price1 * level.Value

while wait() do
	script.Parent.Price.Text = "Price: "..price.." Cash"
	script.Parent.Level.Text = "Your Level: "..level.Value
end

script.Parent.Submit.MouseButton1Click:Connect(function()
	game.ReplicatedStorage.Upgrade:FireServer(player, price)
	print("made it") --this part is not printing
end)
Script that is supposed to receive the event
game.ReplicatedStorage.Upgrade.OnServerEvent:Connect(function(player, price)
	if player.leaderstats.Cash.Value > price - 1 then
		player.leaderstats.Cash.Value -= price
		player.upgradestats.Upgrade.Value += 1
	end
end)
leaderstats
local service = game:GetService("DataStoreService")
local datastore = service:GetDataStore("JumpStore")
local datastore2 = service:GetDataStore("CashStore")

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder", plr)
	leaderstats.Name = "leaderstats"

	local stat = Instance.new("IntValue", leaderstats)
	stat.Name = "Jumps"
	
	local stat2 = Instance.new("IntValue", leaderstats)
	stat2.Name = "Cash"
	
	local data
	local success, errorMessage = pcall(function()
		data = datastore:GetAsync(plr.UserId.."-jumps")
	end)
	
	local data2
	local success2, errorMessage2 = pcall(function()
		data2 = datastore2:GetAsync(plr.UserId.."-cash")
	end)

	if success then
		stat.Value = data
	else
		warn(errorMessage)
	end
	
	if success2 then
		stat2.Value = data2
	else
		warn(errorMessage2)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success, errorMessage = pcall(function()
		datastore:SetAsync(plr.UserId.."-jumps", plr.leaderstats.Jumps.Value) 
	end)

	if not success then
		warn(errorMessage)
	end
	
	local success2, errorMessage2 = pcall(function()
		datastore2:SetAsync(plr.UserId.."-cash", plr.leaderstats.Cash.Value)
	end)
	
	if not success2 then
		warn(errorMessage2)
	end
end)
upgradestats (level)
local service = game:GetService("DataStoreService")
local datastore = service:GetDataStore("UpgradeStore")

game.Players.PlayerAdded:Connect(function(plr)
	local upgradestats = Instance.new("Folder", plr)
	upgradestats.Name = "upgradestats"
	
	local stat = Instance.new("IntValue", upgradestats)
	stat.Name = "Upgrade"
	
	local data
	local success, errorMessage = pcall(function()
		data = datastore:GetAsync(plr.UserId.."-upgrade")
	end)
	
	if success then
		stat.Value = data
	else
		warn(errorMessage)
	end
	
	if stat.Value == 0 then
		stat.Value = 1
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	local success, errorMessage = pcall(function()
		datastore:SetAsync(plr.UserId.."-upgrade", plr.upgradestats.Upgrade.Value) 
	end)
	
	if not success then
		warn(errorMessage)
	end
end)

Ty!

Are there any errors? And does the server receive the message from the client?

What do you mean by “message”? The event? There are 0 errors

We want to know what is that the problem is your script show us that poeming on

This loop runs forever and nothing under it will run. If you have an infinite loop, you need to put it at the bottom of whatever thread it is in. Move it to the bottom of this script.

Ok, I’ll try that and see if it works

  1. local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
  2. local TextService = game:GetService(“TextService”)
  3. local Players = game:GetService(“Players”)
  4. – Define remotes for communication with client
  5. local Remotes = Instance.new(“Folder”, ReplicatedStorage)
  6. Remotes.Name = “PetNamingRemotes”
  7. local UserNamedPet = Instance.new(“RemoteEvent”, Remotes)
  8. UserNamedPet.Name = “UserNamedPet”
  9. –[[
  10. RemoteEvent UserNamedPet
    
  11. Paramaters: petName (string)
    
  12. To be fired by the client when the user has picked a name for their pet.
    
  13. Example:
    
  14. local userNamedPet = ReplicatedStorage:WaitForChild("PetNamingRemotes"):WaitForChild("UserNamedPet")
    
  15. userNamedPet:FireServer("Max")
    
  16. ]]
  17. local SendPetName = Instance.new(“RemoteEvent”, Remotes)
  18. SendPetName.Name = “SendPetName”
  19. –[[
  20. RemoteEvent SendPetName
    
  21. Paramaters: userId (int), petName (string)
    
  22. Fired by the server when a user changes their pet's name
    
  23. Example:
    
  24. local sendPetName = ReplicatedStorage:WaitForChild("PetNamingRemotes"):WaitForChild("SendPetName")
    
  25. sendPetName.OnClientEvent:Connect(function(userId, petName)
    
  26. 	local player = Players:GetPlayerByUserId(userId)
    
  27. 	if player then
    
  28. 		print(player.Name.." has named their pet ".. petName)
    
  29. 	end	
    
  30. end)
    
  31. ]]
  32. local RequestAllPetNames = Instance.new(“RemoteFunction”, Remotes)
  33. RequestAllPetNames.Name = “RequestAllPetNames”
  34. –[[
  35. RemoteEvent SendPetName
    
  36. Paramaters: n/a
    
  37. Fetches the names of every player's pet. To be invoked by the client when a user joins the game. 
    
  38. Example:
    
  39. local requestAllPetNames = ReplicatedStorage:WaitForChild("PetNamingRemotes"):WaitForChild("RequestAllPetNames")
    
  40. local petNames = requestAllPetNames:InvokeServer()
    
  41. for userId, petName in pairs(petNames or {}) do
    
  42. 	local player = Players:GetPlayerByUserId(userId)
    
  43. 	if player then
    
  44. 		print(player.Name.." has a pet named ".. petName)
    
  45. 	end	
    
  46. end
    
  47. ]]
  48. local filterResults = {}
  49. local function broadcastPetName(userId)
  50. local filterResult = filterResults[userId]
    
  51. if filterResult then
    
  52. 	for _, player in pairs(Players:GetPlayers()) do 
    
  53. 		if player then
    
  54. 			-- spawn a new thread so as to not yield the update
    
  55. 			spawn(function()
    
  56. 				-- grab the filtered string for this user
    
  57. 				local toUserId = player.UserId
    
  58. 				local filteredString = filterResult:GetNonChatStringForUserAsync(toUserId)
    
  59. 				filteredString = filteredString or ""
    
  60. 				-- send the remote event
    
  61. 				SendPetName:FireClient(player, userId, filteredString)
    
  62. 			end)
    
  63. 		end
    
  64. 	end
    
  65. end
    
  66. end
  67. UserNamedPet.OnServerEvent:Connect(function(player, petName)
  68. local fromUserId = player.UserId
    
  69. -- pcall to catch errors
    
  70. local success, result = pcall(function()
    
  71. 	return TextService:FilterStringAsync(
    
  72. 		petName, 
    
  73. 		fromUserId
    
  74. 	)
    
  75. end)
    
  76. if success then
  77.    filterResults[fromUserId] = result
    
  78.    broadcastPetName(fromUserId)
    
  79. else
  80.    print("Could not filter pet name")
    
  81. end
  82. end)
  83. RequestAllPetNames.OnServerInvoke = (function(player)
  84. local toUserId = player.UserId
  85. local petNames = {}
  86. – go through filter results and filter the pet name to be sent
  87. for fromUserId, filterResult in pairs(filterResults) do
  88.    local filteredString = filterResult:GetNonChatStringForUserAsync(toUserId)
    
  89.    filteredString = filteredString or ""
    
  90.    -- need to convert userId to string so it can't be sent via a remote function
    
  91.    petNames[tostring(fromUserId)] = filteredString
    
  92. end
  93. return petNames
  94. end)
  95. – Listen for players being removed, and clean up old filter results
  96. Players.PlayerRemoving:Connect(function(oldPlayer)
  97. local userId = oldPlayer.UserId
  98. filterResults(userId) = nil
  99. end)

Maybe you have to try that script

I got an error, ServerScriptService:Upgrade:2: attempt to perform arithmetic (sub) on Instance and number

I don’t think that script will be helpful to his problem. It’s for something completely different and it is formatted poorly. That script was probably for a pet system you copied and pasted without knowing what it actually is.

But we know this the problem is coming to error the script is if you enter that maybe will come to me ever

This is the line where it says the error occured

if player.leaderstats.Cash.Value > price - 1 then

But we know is that big problem my steps it is it’ll but if you try that maybe we will fix that problem I will see

price is a reference to an instance, you may need to do price.Value instead if it is a reference to a value object (IntValue, NumberValue etc.).

1 Like

Change the local script to fire (price).

When the server receives the message it will automatically add another parameter before the price with the player.

These are the 2 lines where Price is defined

local price = price1 * level.Value
local price1 = 150
:FireServer(player, price)

The player parameter here isn’t required, the client’s (which fired the server) player object is automatically passed to the server.

1 Like

Ok, i removed the player parameter and it works now except for the price text label

local price = price1 * level.Value

while wait() do
	script.Parent.Price.Text = "Price: "..price.." Cash"

The value of price is only assigned once, you need to reassign it whenever necessary.

2 Likes

Price1 was predefined as 150.

Basically it’ll keep updating it, given that the price is 150. You’ll need to update the variable using a function.