This script is supposed to fire a remote function. Line 15: print(selectedItem2.Value) does work, but the line after it which fires the remote function does not seem to work. I added some prints to the server script and nothing was printed. (let me know if you want to see anything else) Thanks
local player = game.Players.LocalPlayer
local playerName = player.Name
local replicatedStorage = game:GetService("ReplicatedStorage")
local selectedItem2 = script.Parent.Parent.Parent:WaitForChild("SelectedItem2")
local success--Variable
script.Parent.MouseButton1Click:Connect(function()
success = false--Makes success false
if selectedItem2.Value ~= nil then--If the value isn't equal to nil
selectedItem2.Value = script.Parent.Parent.Name
print(selectedItem2.Value)
success = replicatedStorage.Equip:InvokeServer(selectedItem2.Value)--Make a request to the remote function
end
if success then
print("Equiped!")
else
print("Equip Failed!")
end
end)
ServerScript
game.ReplicatedStorage.CheckSale.OnServerInvoke = (function(player,item)--Responds to the remote function request
local ServerStorage = game:GetService("ServerStorage")
local shopItems = game:GetService("ServerStorage"):WaitForChild("ShopItems")--Defines the items in ServerStorage
print("Yo")
if not player.Backpack:FindFirstChild("LongStick") then--Check to see if the player has enough money
print("Ello")
local gear = shopItems[item]:FindFirstChildOfClass("Tool"):Clone()--Clones the item they bought
gear.Parent = player.Backpack--Put the clone in their backpack
print("Ye")
return true
else
return false
end
end)
I love to optimize scripts to the best efficiency. I would suggest you use GetService for the player just in case it errors which it would not, but still counts.
Use:
remote.OnServerInvoke = fucntion(args))
And here is your whole script optimized.
local Players= game:GetService("Players")
local Player = Players.LocalPlayer
local playerName = player.Name
local replicatedStorage = game:GetService("ReplicatedStorage")
local selectedItem2 = script.Parent.Parent.Parent:WaitForChild("SelectedItem2")
local success
script.Parent.MouseButton1Click:Connect(function()
success = false --Makes success false
if selectedItem2.Value ~= nil then--If the value isn't equal to nil
selectedItem2.Value = script.Parent.Parent.Name
print(selectedItem2.Value)
success = replicatedStorage.Equip:InvokeServer(selectedItem2.Value)
end
if success then
print("Equiped!")
else
print("Equip Failed!")
end
end)
Can’t optimize much due to being optimized already except for using a variable for Player’s service. Also, why are you setting the variable to false? Variable’s not defined are nil, meaning nothing.
Please show your server script which is the event listener, there is nothing wrong with your local one.
It seems that one or both of the two variables at the start of the remote probably are yielding for smth that does not exist (waitforchild) and thus you see nothing printed. This would be the likely cause if you see no errors in the console.
I added print("9") to the second line right after the server is invoked and it doesn’t print. I think something is happening before the variables, but not sure what.
game.ReplicatedStorage.CheckSale.OnServerInvoke = (function(player,item)--Responds to the remote function request
print("9")
local ServerStorage = game:GetService("ServerStorage")
local shopItems = game:GetService("ServerStorage"):WaitForChild("ShopItems")--Defines the items in ServerStorage
print("Yo")
if not player.Backpack:FindFirstChild("LongStick") then--Check to see if the player has enough money
print("Ello")
local gear = shopItems[item]:FindFirstChildOfClass("Tool"):Clone()--Clones the item they bought
gear.Parent = player.Backpack--Put the clone in their backpack
print("Ye")
return true
else
return false
end
end)
Your variables are messed up and maybe that’s the cause and that ) before function.
game.ReplicatedStorage.CheckSale.OnServerInvoke = function(player,item)--Responds to the remote function request
print("9")
local ServerStorage = game:GetService("ServerStorage")
local shopItems = ServerStorage:WaitForChild("ShopItems")--Defines the items in ServerStorage
print("Yo")
if not player.Backpack:FindFirstChild("LongStick") then--Check to see if the player has enough money
print("Ello")
local gear = shopItems[item]:FindFirstChildOfClass("Tool"):Clone()--Clones the item they bought
gear.Parent = player.Backpack--Put the clone in their backpack
print("Ye")
return true
else
return false
end
end)
You using :GetService 2 times, I fixed it and run the script again.