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

Cloning a hat to the character with Filtering Enabled?

Cloning a Hat from the ServerStorage into the Character with Filtering Enabled.

When I click the button, the StringValue's value becomes the name of the frame and then through the Filtering Enabled, I'm trying to read the name of the value located in the client and search it in the ServerStorage and cloning it in the character.

This is the output I'm getting: ServerScriptService.Filtering Enabled:26: attempt to index a nil value

Part of the script in the Client

local hat = player.PlayerGui:WaitForChild("ScreenGui"):WaitForChild("Hat")
hat.Value = script.Parent.Parent.Name
game.Workspace.FE.Hat:InvokeServer()

In the Server Script

function FE.Hat.OnServerInvoke(player)
	local Hat = player.PlayerGui.ScreenGui.Hat
	game.ServerStorage:FindFirstChild(Hat.Value):Clone().Parent = player.Character
end

3 comments

0

Which line here coresponds to line 26 in your 'Filtering Enabled' script under ServerScriptService?

0

Line 3 of the Server Script: game.ServerStorage:FindFirstChild(Hat.Value):Clone().Parent = player.Character

0

It is likely that the returned value from FindFirstChild is nil

1 answer

✓ Accepted answer

You could try passing the Hat value through the invocation as an argument.

Client:

local hat = player.PlayerGui:WaitForChild("ScreenGui"):WaitForChild("Hat")
hat.Value = script.Parent.Parent.Name
game.Workspace.FE.Hat:InvokeServer(hat.Value)

Server:

function FE.Hat.OnServerInvoke(player, hat)
	local Hat = game.ServerStorage:FindFirstChild(hat)
	if Hat then
		Hat:Clone().Parent = player.Character
	end
end

I don't know which line is line 26 so I don't know if that was the problem. If not, please elaborate. Hope this helped.

Explanation Edit: On the client, hat.Value is passed to the invocation as whatever the value type is (in this case, a string). The server doesn't even know there's a ValueObject involved-- all it receives is the value.

Let's say hat.Value == "Roblohunk". On line 3 of the LocalScript, before even invoking the server, the script gets the value of hat.Value-- "Roblohunk"-- and then passes it to the server. So, if hat.Value == "Roblohunk", line 3 is basically:

game.Workspace.FE.Hat:InvokeServer("Roblohunk")

This then passes to the OnServerInvoke function which, as you know, automatically takes the player who invoked as a parameter. That means the argument we passed is the second one, which I specified as 'hat'.

Considering the previous example where hat.Value == "Roblohunk", here's an explanation of the function:

function FE.Hat.OnServerInvoke(player, hat)
	local Hat = game.ServerStorage:FindFirstChild(hat) -- Find the first child named "Roblohunk" (or whatever hat.Value is) in ServerStorage. If a match isn't found, :FindFirstChild() returns nil. So the 'Hat' variable either becomes the hat, or nil.
	if Hat then -- We check to see if the hat exists (if it found "Roblohunk" in ServerStorage).
		Hat:Clone().Parent = player.Character -- If it does, we clone it and set its parent to player.Character.
	end
end

Hope this helped to clear it up a bit.

5 comments

0

Thanks you're a life saver, it worked although I don't know what you did exactly, how did the code recognize hat as hat.Value? Once again thanks a lot because it worked!

0

(by hat I mean to the argument the one in the brackets)

0

Edited.

0

Thanks a lot!

0

No problem.

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