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

How do I make this script work for onTouch and ReplicatedStorage?

I made this by making a player touch the snow, and once they touch it the Snowball will come to their backpack. But apparently it didn't work for some reason. But this is the script and how I set it up in workspace.

Workspace:

-- Snow (Model)
 -- Snow (Part)
  -- Script (The one that I was talking about)

Script:

script.Parent.Snow:connect(onTouch)
    if snow.Name == "Snowball" then
        print("Snowball given")
        g = game.ReplicatedStorage:FindFirstChild("Snowball")  -- Item in ReplicatedStorage
        g:Clone().Parent = player.Backpack
    else print("Failed")
    end
end)

I hope these gave a good explanation. Thank you!

2 answers

To start off, the main reason this won't run is due to the first line.

script.Parent.Snow:connect(onTouch)

This line doesn't have an event to listen for, thus nothing will ever occur.

To listen for the touched event use this:

script.Parent.Touched:connect(onTouch)

Now the line still isn't fixed, you must nest the function, unlike trying to do what you did.

script.Parent.Touched:connect(function (hit)

This now is a nested function, which seems as what you were trying to accomplish, and the "hit" variable is the part that touched the snow.

You also never defined the player variable (line 5), so it wouldn't work, rather error. So before line 5 I'll be using an iterator to check if the player exists.

	for _, player in pairs(game.Players()) do
		if player:IsA("Player")  and player.Name == hit.Parent.Name then
			g:Clone().Parent = player
			return
		end
	end

Hopefully this works, and (I hope) a decent explanation for how this works.

If there are any errors, tell me, I reply asap.

Yeah, the problem is that you don't have an event but, that's not the only problem. Because Regular Scripts cannot use ReplicatedStorage and if you put a LocalScript inside of anything that isn't a descendant of a player, than it doesn't run.


#Touched This event is needed for your script to run, we should ad a debounce so the player doesn't get 7 snowballs at a time. This also errored because Snow is the script's parent, but you're trying to find snow inside of the part.

script.Parent.Touched:connect(function(snow)
	--Code
end)

#ReplicatedStorage This can only be accessed by a Local Script

--Local Script
game:GetService("ReplicatedStorage")

#Solutions

  • Use ServerStorage Instead
  • Use RemoteFunctions


@Solution 1 Use ServerStorage:

script.Parent.Touched:connect(function()
    if snow.Name == "Snowball" then --What is snow?
        g = game.ServerStorage:FindFirstChild("Snowball")  -- Item in ServerStorage
        g:Clone().Parent = player.Backpack --What is "player"
    end
end)

#Solution 2 Use RemoteFunctions! Insert one into the touched script

--Regular Script
script.Parent.Touched:connect(function(p)
	local plyr = game:GetService("Players"):GetPlayerFromCharacter(p.Parent)
    if snow.Name == "Snowball" and plyr  then --What is snow?
        script.RemoteFunction.InvokeClient(plyr).Parent = plyr.Backpack
    end
end)

In LocalScript inside Player:

local rf = workspace.Part.Script.RemoteFunction --Fix the hierarchy

rf.OnClientInvoke:connect(function()
	return game.ReplicatedStorage:FindFirstChild("Snowball"):clone()
end)


Hope it helps!

3 comments

0

ReplicatedStorage can be accessed by both the server and the client.

0

@ImageLabel, so I've been lied to :/

0

Lol.

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