I was making a game but I came across an error when I was making a admin panel from scratch. I was stuck with this for a lot of weeks to research. If you could help it would be greatly appreciated!
local script (in TextLabel):
local player = game.Players.LocalPlayer local ReplicatedStorage = game:GetService("ReplicatedStorage") local createPartEvent = game.ReplicatedStorage.Hi local Amount = script.Parent.Panel.Buttons.Cash.CashAmount local Value = script.Parent.Panel.Buttons.Cash.CashType local buttons = script.Parent.Panel.Buttons.Cash buttons.MouseButton1Click:Connect(function() createPartEvent:FireServer(Amount, Value) end)
script:
local ReplicatedStorage = game:GetService("ReplicatedStorage") local createPartEvent = game.ReplicatedStorage.Hi createPartEvent.OnServerEvent:Connect(function(player, text, value) local Stats = player:WaitForChild("Stats") print("recieved") wait(.1) if text == "Cash" then Stats.Cash.Value = value print("Work") elseif text == "Multiplier" then Stats.Multiplier.Value = value print("Work") end end)
(output dosent have errors)
The interpretation of the remote function that you write is different.
The thing you are firing is (CashAmount, CashType) BUT the interpretation of onServerEvent in server script just interprets CashAmount as text instead of CashType. That's why it did print "received" but never "work". EDIT: The value sent is the instance NOT the value of the instance.
Confusing right? Here is the overall explanation which is easy to understand
createPartEvent:FireServer(Amount, Value)
You fire amount as CashAmount and you fire value as CashType. The value sent is always instance and never value of the instance.
createPartEvent.OnServerEvent:Connect(function(player, text, value)
-- This is server side function
player
parameter is unused
the server received text
as amount (cashAmount) instead of cashType which value
instead receive Value (cashType). The server also get text
and value
as an instance
You are trying to compare CashAmount to text which supposed to check CashType, that's why the check is failed. You can even put else
and use print("Not Work!") and it will output Not Work! EDIT: Ignore the italic text, You are trying to compare the value sent (Instance) to a number which never works because the value is different when gotten from the client.
== First Solution for your code (Server Side) ==
-- SERVER SIDE SOLUTION NEVER WORKS AFTER MORE INFORMATION --
-- THE SERVER-SIDE SOLUTION BELOW IS ARCHIVED --
-- USE CLIENT-SIDE SOLUTION INSTEAD OF SERVER-SIDE IF YOU HAVE USED SERVER-SIDE SOLUTION THEN YOU HAVE TO REVERT THE CHANGE--
local ReplicatedStorage = game:GetService("ReplicatedStorage") local createPartEvent = game.ReplicatedStorage.Hi createPartEvent.OnServerEvent:Connect(function(player, value, text) -- Switching the parameter local Stats = player:WaitForChild("Stats") print("recieved") wait(.1) if text == "Cash" then Stats.Cash.Value = value print("Work") elseif text == "Multiplier" then Stats.Multiplier.Value = value print("Work") end end)
== Second Solution (Client Side) ==
-- THE SOLUTION HAVE BEEN UPDATED IN 15TH APRIL 2021 (EUROPEAN TIME) --
local player = game.Players.LocalPlayer local ReplicatedStorage = game:GetService("ReplicatedStorage") local createPartEvent = game.ReplicatedStorage.Hi local Amount = script.Parent.Panel.Buttons.Cash.CashAmount local Value = script.Parent.Panel.Buttons.Cash.CashType local buttons = script.Parent.Panel.Buttons.Cash buttons.MouseButton1Click:Connect(function() createPartEvent:FireServer(Value.Value, Amount.Value) -- Added value end)
NOTE: You can only choose the client-side solution.
In the future, you probably want to check the parameter that client/server fire or received and make sure that server/client interpret the fired message correctly.
EDIT: Also check if you're trying to fire the value of the instance instead of the instance itself in the future too! you can check the value by using the print()
function such as print(Value)
. This is really useful in helping debugging bugs.
Happy Scripting!
I think I see a problem here. You see, in the local script, you didn't reference what the "text" parameter is so the serverscript thinks that text is just nil.