Im making a game and i have a remote event (BuyBot), and a local script in the player's startergui, and a script in serverscriptservice. Now, When i fire the event from the localscript, I check that the variable (bots) is 50. But when i fire the event, The script receives it and changes 50 to hasanchik. I have not seen anything like this ever before, Roblox refuses to work! HELP?
Localscript:
local ReplicatedStorage = game:GetService("ReplicatedStorage") local remoteEvent = ReplicatedStorage:WaitForChild("BuyBots") function click() ----print("Clicked") bots = 50 print(bots) remoteEvent:FireServer(bots) end script.Parent.MouseButton1Down:Connect(click)
Script:
local ReplicatedStorage = game:GetService("ReplicatedStorage") local remoteEvent = ReplicatedStorage:WaitForChild("BuyBots") function buyBots(bots) print(bots) for i = 1, #bots do bot = game.ServerStorage.Dummy:Clone() names = require(workspace.NPCFolder.Names) name = names[math.random(1,#names)] bot.Name = name Bc = bot["Body Colors"] legsColor = BrickColor.Random() headArmsColor = BrickColor.Random() torsoColor = BrickColor.Random() Bc.HeadColor = headArmsColor Bc.LeftArmColor = headArmsColor Bc.LeftLegColor = legsColor Bc.RightArmColor = headArmsColor Bc.RightLegColor = legsColor Bc.TorsoColor = torsoColor bot.Parent = workspace.NPCFolder.Bots end end remoteEvent.OnServerEvent:Connect(buyBots)
in line 4 of the script, It prints "hasanchik" instead of "50". And the error it outputs is ServerScriptService.AddBot:5: 'for' limit must be a number
Bots
is a numerical userdata, calling hash, or the length-of operator will fail here as it call only be applied in the scenario of finding the length of a String or Table. The first argument sent by FireServer
will always be the Player that called it, so you’re trying to use a Player Object as a form of defining how many iterations you wish to perform with the for
loop, as the Player Object was pushed into the bots
parameter.
Since we don’t appear to need to use the Player
that gets imposed as the first parameter, nor the i
variable, we can use the special character _
to tell the Lua Compiler to ignore them
Using all the information above, we can modify your code to solve the Logic error:
local ReplicatedStorage = game:GetService("ReplicatedStorage") local RemoteEvent = ReplicatedStorage:WaitForChild("BuyBots") --// Use _ to tell Lua Complier to ignore the first parameter RemoteEvent.OnServerEvent:Connect(function(_,bots) --// Use _ to tell Lua Compiler to not store i for _ = 1,bots do end end)