So I am creating a tool giver system, and there are a remote function and a local script. Whenever you click a button it should invoke the remote function but while doing this the script errors: 19:40:29.649 - ServerScriptService.Tool:19: attempt to compare string with a number.
Please Help -- Yours truly, suspectshot108
Local Script: Broken -- Line.. uhh well whenever u invoke the server.
local EventFolder = game:GetService("ReplicatedStorage").Functions local RequestTool = EventFolder:WaitForChild("RequestTool") for _,btn in pairs(script.Parent:GetChildren()) do if btn:IsA("TextButton") and btn.Name == "Item" then btn.MouseButton1Click:Connect(function() local Cost = btn:WaitForChild("Cost").Value local Name = btn:WaitForChild("Name").Value RequestTool:InvokeServer(Name,Cost) end) end end
Script: Works Fine -- I think...
--[[ File Name: Tool Request Accepted; Writen by suspectshot108; Version: 1.0 --]] local EventFolder = game:GetService("ReplicatedStorage").Functions local RequestTool = EventFolder:WaitForChild("RequestTool") RequestTool.OnServerInvoke = function(Player,Name,Cost) if Player ~= nil then if Player.leaderstats and Player.Security then if Player.leaderstats.Bits and Player.Security.Bits then if string.match(Player.leaderstats.Bits.Value,Player.Security.Bits.Value) then local Ls = Player.leaderstats local Sy = Player.Security local Tl local Ne = "Bits" if Ls[Ne].Value and Sy[Ne].Value >= Cost then Tl = game:GetService("ReplicatedStorage").Tools[Name]:Clone() Tl.Parent = Player.Backpack wait() Ls[Ne].Value = Ls[Ne].Value - Cost Sy[Ne].Value = Sy[Ne].Value - Cost end else Player:Kick("Something is wrong with your leaderstats.") end end end end end
From what I understand, you are using StringValue instead of IntValue, it is recommended that you change its value to IntValue.
Errors: You should use IntValue instead of StringValue to use Values with numbers. but there are ways to do it with StringValue. If you want to use StringValue you need to use tostring() and tonumber()
however if you want to continue with StringValue you can try this:
You can see tostring() and tonumber() here: tonumber() - tostring()
For this you need to detect with number, and change with string.
Fixed script:
--[[ File Name: Tool Request Accepted; Writen by suspectshot108; Version: 1.0 --]] local EventFolder = game:GetService("ReplicatedStorage").Functions local RequestTool = EventFolder:WaitForChild("RequestTool") RequestTool.OnServerInvoke = function(Player,Name,Cost) if Player ~= nil then if Player.leaderstats and Player.Security then if Player.leaderstats.Bits and Player.Security.Bits then if string.match(Player.leaderstats.Bits.Value,Player.Security.Bits.Value) then local Ls = Player.leaderstats local Sy = Player.Security local Tl local Ne = "Bits" if Ls[Ne].Value ~= nil and Sy[Ne].Value ~= nil then if tonumber(Ls[Ne].Value) and tonumber(Sy[Ne].Value) >= Cost then Tl = game:GetService("ReplicatedStorage").Tools[Name]:Clone() Tl.Parent = Player.Backpack wait() local LSnE = Ls[Ne].Value local SYnE = Sy[Ne].Value Ls[Ne].Value = Ls[Ne].Value - tostring(Cost) Sy[Ne].Value = Sy[Ne].Value - tostring(Cost) end end else Player:Kick("Something is wrong with your leaderstats.") end end end end end
Hope it helped! :)
So since the problem is in the server script on line 19, that means there's a flaw in . . .
if Ls[Ne].Value and Sy[Ne].Value >= Cost then
I think the problem is that you set Ne, to a string, aka a text, and Ls to leaderstats. I'm not sure what kind of object leaderstats is, but it's probably not a number. I'm also assuming that "Cost" is a number. So the error is saying you're trying to ask if a string is greater than or equal to a number. I don't really understand what exactly you're trying to do with the script, but just pay attention to that, and try to figure it out from there.