I have a ShopGui and the main script in there is working and Firing a RemoteEvent and the script for the Event is in ServerScriptService. The shop item is in the gui in a folder. Here is the main script:
--// Variables local sgui = script.Parent.Parent local misc = sgui.Misc local guis = sgui.Guis local scripts = sgui.Scripts local values = sgui.Values local I1 = guis.Trails.Item1:WaitForChild("BuyT") local I2 = guis.Trails.Item2:WaitForChild("BuyT") local I3 = guis.Trails.Item3:WaitForChild("BuyT") local I4 = guis.Trails.Item4:WaitForChild("BuyT") local I5 = guis.Trails.Item5:WaitForChild("BuyT") local I6 = guis.Trails.Item6:WaitForChild("BuyT") local I7 = guis.Trails.Item7:WaitForChild("BuyT") local I8 = guis.Trails.Item8:WaitForChild("BuyT") local I9 = guis.Trails.Item9:WaitForChild("BuyT") local IX = guis.Trails.ItemX:WaitForChild("BuyT") local bt = game:GetService("ReplicatedStorage").BuyTrail local mb = game:GetService("ReplicatedStorage").MoveBlur --// Code I1.MouseButton1Click:Connect(function() bt:FireClient("Trail1") end) I2.MouseButton1Click:Connect(function() bt:FireClient("Trail2") end) I3.MouseButton1Click:Connect(function() bt:FireClient("Trail3") end) I4.MouseButton1Click:Connect(function() bt:FireClient("Trail4") end) I5.MouseButton1Click:Connect(function() bt:FireClient("Trail5") end) I6.MouseButton1Click:Connect(function() bt:FireClient("Trail6") end) I7.MouseButton1Click:Connect(function() bt:FireClient("Trail7") end) I8.MouseButton1Click:Connect(function() bt:FireClient("Trail8") end) I9.MouseButton1Click:Connect(function() bt:FireClient("Trail9") end) IX.MouseButton1Click:Connect(function() bt:FireClient("TrailX") end)
And here is the event script:
local bt = game:GetService("ReplicatedStorage").BuyTrail local mb = game:GetService("ReplicatedStorage").MoveBlur bt.OnClientEvent:Connect(function(player, trail) print("Attempting To Buy Trail") local t = player.PlayerGui.TrailShop:WaitForChild("Trails"):FindFirstChild(trail):Clone() player.CharacterAdded:Connect(function(char) t.Parent = char.HumanoidRootPart local a0 = Instance.new("Attachment",char.Head) a0.Name = "Attach0" local a1 = Instance.new("Attachment",char.HumanoidRootPart) a1.Name = "Attach1" t.Attachment0 = a0 t.Attachment1 = a1 end) end) mb.OnServerEvent:Connect(function() end)
I get an error when I click the button that says "Unable to cast value to Object" for whatever line the button I clicked on for example if I clicked the first one it would be on line 24.
I'm really not sure what is happening as I am new to FE scripting. Some help would be appreciated. And if you could test it before you post it to make sure it works.
Take this if you need a better reference: https://www.roblox.com/library/1111083460/Shop-Need-Help
The error is that the first argument of each FireClient
function expects a player, and so Roblox is trying (and failing) to convert the string you've provided into a player. Instead, use FireAllClients
.
Do note that server scripts do not usually have access to a player's GUI. The server should handle purchases, awarding players items, etc, and the client should handle the GUI completely. The client must notify the server when a player wishes to buy something, for instance.
The same script should not have both OnClientEvent
and OnServerEvent
-- either's a server script or a client script, never both (except for the special case when you're testing it in Studio without using the "Start Server" option).
[Edit]
You requested help fixing your scripts, so I did (and uploaded a fixed version here: https://www.roblox.com/library/1114647229/Ki-ngPhantoms-Shop-Fixed
(Note that I only did this because I felt like it -- this site is not a request site. You're supposed to try and make the changes yourself and then ask for help when you don't know how to proceed.)
If you want to get good at scripting, study the scripts and try to understand them (and feel free to ask about them on this site, or even PM me. You can also email me at [email protected]. Some topics you'll want to learn about if you want to get better:
Part
, Frame
, Trail
, etc. Some important definitions to understand my comments:
private
: Means that the function or variable is not meant to be used outside the class.public
: Means that the function or variable is meant to be used outside the class.Other notes about the scripts I've provided:
Few things, Cant do OnClientEvent in a Server script you firing to the client from a local script and in the server script you need a player as your first argument too