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

Compare Instance with Number?

I tried do to a Gate Script and now it says "ServerScriptService.HalloweenGates Unlock:7: attempt to compare string <= number " The Error is in ServerScriptService Line 7 Pls help me

Script (Local) :

local Activator = game.Workspace:WaitForChild("Haunted Gate Gate").Activator --Gate
local GuiText = "Do you want to buy the Haunted Gate?" --Text For The Gui
local Price = 100000 --Price
local GateName = "HauntedEntrance" --Name to unlock

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvent = ReplicatedStorage:WaitForChild("HalloweenGates")

Activator.Touched:Connect(function(hit)
	if hit.Parent == game.Players.LocalPlayer.Character then
	print("Touched")
	local player = game.Players.LocalPlayer
	remoteEvent:FireServer(player, GuiText, Price, GateName)
	end
end)

Script (ServerScriptService) :

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild("HalloweenGates")
local TakeMoney = ReplicatedStorage:WaitForChild("HalloweenGatesTakeMoney")

remoteEvent.OnServerEvent:Connect(function(player, GuiText, Price, GateName)
	if player.EventMoney.Sweets.Value >= Price then
		player.StarterGui.HalloweenGates.Frame.Visible = true
		player.StarterGui.HalloweenGates.Shadow.Visible = true
		player.StarterGui.HalloweenGates.Frame.Price.Text = "This Gate costs"..Price.."Coins"
		player.StarterGui.HalloweenGates.Frame.Buy.Gate.Value = GateName
		player.StarterGui.HalloweenGates.Frame.Buy.Price.Value = Price
	end
end)

TakeMoney.OnServerEvent:Connect(function(player, Price)
	player.EventMoney.Sweets.Value = player.EventMoney.Sweets.Value - Price
end)

1 answer

on your Fire Server function in your local script you have defined the player but the RemoteEvent object automatically knows who has called the remote event, so you don't need to specify the player from your local script!.

What's actually happening is as your server-side script is getting the player automatically and shifting each variable over so it sees two variables as the player object.

The data your sending to the server script is as follows:

	.....
    remoteEvent:FireServer(player, GuiText, Price, GateName)
	....

this is what the server-side script sees:

remoteEvent.OnServerEvent:Connect(function(player, player, GuiText, Price, GateName)

So to simply fix your script, simply change this:

Activator.Touched:Connect(function(hit)
    if hit.Parent == game.Players.LocalPlayer.Character then
    print("Touched")
    local player = game.Players.LocalPlayer
    remoteEvent:FireServer(player, GuiText, Price, GateName)
    end
end)

To this:

Activator.Touched:Connect(function(hit)
    if hit.Parent == game.Players.LocalPlayer.Character then
    print("Touched")
    local player = game.Players.LocalPlayer
    remoteEvent:FireServer(GuiText, Price, GateName) -- no need to define the player as its already sent to the server via the RemoteEvent!
    end
end)

Hope this helps! :)

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