Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

[Please help] RemoteEvent server script does not recognize value set in local script?

I have a leveling up system that plays a cool little animation when the player levels up, and it works nicely, however the problems start when it uses a remote event to reward the player. Here is my script:

local player = game.Players.LocalPlayer
local title = script.Parent.LevelTitle
local num = script.Parent.LevelNum
local reward1 = script.Parent.CashRew

local level = game.Players.LocalPlayer.LevelingSystem.Level
local event = game.ReplicatedStorage.RewardRE.LevelUpReward


level:GetPropertyChangedSignal("Value"):Connect(function()
	local rew = math.random(325,550)
	num.Text = level.Value - 1
	title.Visible = true
	title:TweenPosition(UDim2.new(0.501, 0,0.313, 0), "Out", "Quad", 0.5)
	wait(1)
	num:TweenPosition(UDim2.new(0.5, 0,0.406, 0), "Out", "Quad", 0.5)
	wait(1)
	num.Text = num.Text + 1
	wait(1)
	reward1.Text = "+ $"..rew
	reward1:TweenPosition(UDim2.new(0.5, 0,0.486, 0), "Out", "Quad", 0.5)
	wait(2)
	reward1:TweenPosition(UDim2.new(0.5, 0,-1.486, 0), "Out", "Quad", 0.5)
	title:TweenPosition(UDim2.new(0.501, 0,1.313, 0), "Out", "Quad", 0.5)
	num:TweenPosition(UDim2.new(0.5, 0,1.406, 0), "Out", "Quad", 0.5)
	print(rew)
	event:FireServer(player, rew)
end)

Here is the server script:

local event = game.ReplicatedStorage.RewardRE.LevelUpReward

event.OnServerEvent:Connect(function(player, rew)
	local player = game.Players:FindFirstChild(player.Name)
	print(rew)
	player.leaderstats.Cash.Value = player.leaderstats.Cash.Value + rew
	print("Awarded "..tostring(player.Name).." $ "..rew)
end)

When I ask to print what "rew" is (rew is the math.random value that is supposed to be the players reward) it says that rew is the players name, and then errors because you cannot perform arithmetic with an Instance. Please help, I don't know what is wrong.

1 answer

✓ Accepted answer

That's because you're firing over a Player argument on the client, you do not need to do this as RemoteEvents automatically fire over the player.

So instead of:

event:FireServer(player, rew)

Just send over:

event:FireServer(rew)

1 comment

0

thank you very much

This discussion is preserved as it was. New answers, comments, and votes are closed.