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

(easy Script) i am trying to make a gui button that gives you a tool or a gun but not working?

Problem: i made this simple script but it seems to not work when you press the button you wont get the tool when you press the text button

Script:

local StarterPack = game.StarterPack

script.Parent.MouseButton1Click:Connect(function()
	StarterPack.HyperlaserGun.Enabled = true
	
end)

2 answers

✓ Accepted answer

Alright, backtrack here.

Tools placed in StarterPack should replicate to the player's Backpack when they join the game and when their character spawns in. If you don't want a tool to be immediately equipped like this, you place it in a storage. Which storage? Preferrably ServerStorage, since ReplicatedStorage isn't a very secure place to keep tools.

Ok, now let's say you have a tool located in ServerStorage you want to give to the player. Adjust your PlayerAdded function so that it clones the tool out of ServerStorage and place it in the player. (Mind you, the script needs to be a normal script; local scripts can't access ServerStorage.) Should be a relatively simple fix.

In your case, you'd want the client to activate the signal to acquire the tool. For this, you'd utilize a RemoteEvent so the client sends the signal to the server, and the server is able to clone the tool into that player (RemoteEvent:FireServer() will feed the server the client that fired the event). Place the RemoteEvent in ReplicatedStorage so both the client and the server can access the event.

Example usage of a RemoteEvent, just to show you how to use one:

Client

local event = game:GetService("ReplicatedStorage"):WaitForChild("Event")
local msg = "Hello"
script.Parent.MouseButton1Click:Connect(function()
	event:FireServer(msg)
end)

Server

local event = game:GetService("ReplicatedStorage"):WaitForChild("Event")
event.OnServerEvent:Connect(function(client, msg) -- "client" refers to the player who fired the event
	print(msg)
end)

1 comment

0

That code is nice!

You'd need to clone the tool to the player's backpack so they can get it.

local StarterPack = workspace:WaitForChild('your tool name') -- or change to the directory of your tool.

script.Parent.MouseButton1Click:Connect(function()
    StarterPack:Clone().Parent = game:GetSerivce("Players").LocalPlayer.Backpack -- Clones the tool in the starterpack and to the player's backpack, although you should put the tool somewhere else
end)

Also, since the gun most likely has serverscripts, you should use remoteevents for this. Hope this helps!

5 comments

0

i hope you realize stuff in starterpack automatically gets replicated to the backpack.. theres no point in this

0

Every tool that is inserted into the StarterPack is automatically replicated. This code is wrong and redundant.

0

I do, I just wasn't sure if I should edit it as I just edited what needed to be fixed. although I did edit it again, so taking out the downvotes would be appreciated.

0

:clone() makes an object not tent to work

0

Because it's a localscript, localscripts can't copy the serverscripts in the tool. You'd need to use remoteevents

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