Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

When I test the script, I am not getting spawned to the spawns and not getting the tools, help?

Asked by
Exteezy -3
5 years ago
Edited 5 years ago
local repstorage = game:GetService("ReplicatedStorage")
spwns = game.Workspace.spawns
Brick = script.Parent
randomspwn = spwns[math.random(1, #spwns)]

Brick.Touched:connect(function(part)
    local clonegpr = repstorage:WaitForChild('GPR'):Clone().Parent = game.Players.LocalPlayer.Backpack
    local clonepstl = repstorage:WaitForChild('PSTL'):Clone().Parent = game.Players.LocalPlayer.Backpack
if part.Parent.Humanoid then
part.Parent:MoveTo(randomspwn.Position)
end
        end
    end)
0
put the code between the ~~~ lines but not on the same line as the ~~~ User#5423 17 — 5y

1 answer

Log in to vote
0
Answered by
Lugical 425 Moderation Voter
5 years ago
Edited 5 years ago

Alright, so from what I see, there are 3 main issues that I see.

1. You're technically only referring to one part in this instance, and can be faulty. (I'll explain later)

2. Deprecated terms. (Minor)

3. The way you're cloning these tools into the client isn't the best way from what I know.

Now, let's start with the first major issue:

spwns = game.Workspace.spawns
randomspwn = spwns[math.random(1, #spwns)]

The main issue with this is that Workspace will only look for one part, and then stop. This isn't efficient and wanted at all, as it can lead to underlying problems. Instead, what would be best would be to group your "swpn" parts into a Folder located in Workspace. With this, we can do a for i, v in pairs() loop to find a random spawn. (we could also rename the parts to help us out, like for example, spawns1, spawns2, etc...)

--The for loop done to find the random spawn
for i, v in pairs(game.Workspace.Folder:GetChildren) do
    local randomBreak = math.random(1, 5) --Change the 5 to the number of spawns
    local numberName = tostring(randomBreak) -- This allows the number to be a piece of text
    if v.Name == "spawns"..numberName then --Checks if the part matches the part wanted.
        --Put the rest in here!
        wait()
    end
end

Now, the other issue was deprecated terms. I just want to note that :connect is deprecated and is not wanted for current code usage. It's best to use :Connect instead.

Lastly, the other issue is how you're cloning the tools in.

 local clonegpr = repstorage:WaitForChild('GPR'):Clone().Parent = game.Players.LocalPlayer.Backpack
    local clonepstl = repstorage:WaitForChild('PSTL'):Clone().Parent = game.Players.LocalPlayer.Backpack

You can't access the LocalPlayer but you can instead create a RemoteEvent to send stuff.

Now, this is the corrected script that should inform you more of what to do next time!

--SERVER SCRIPT
local repstorage = game:GetService("ReplicatedStorage")
Brick = script.Parent


Brick.Touched:Connect(function(part) --Fixed the :connect with :Connect
    if part.Parent.Humanoid then
        local player = game.Players:GetPlayerFromCharacter(part) -- Finds the character
        local randomBreak = math.random(1, 5) --Change the 5 to the number of spawns
        local numberName = tostring(randomBreak) -- Allows number to be a piece of text
        for i, v in pairs(game.Workspace.Folder:GetChildren) do
            if v.Name == "spawns"..numberName then --Checks if part is the right one.
                part.Parent:MoveTo(v.Position) --Moves it
            break --Stops the loop
            end 
            wait()
        end
        repstorage:WaitForChild("RemoteEvent"):FireClient(player) --Sends message to player that touched it
         end
    end)
--NOTES
--Make sure you put the spawns in a folder in Workspace
--Create a RemoteEvent in ReplicatedStorage
--LOCAL SCRIPT
--Place it in StarterPlayerScripts

local repstorage = game:GetService("ReplicatedStorage")

repstorage.RemoteEvent.OnClientEvent:Connect(function(player) -- Receives message
     local clonegpr = repstorage:WaitForChild('GPR'):Clone()
    clonegpr.Parent = game.Players.LocalPlayer.Backpack
         local clonepstl = repstorage:WaitForChild('PSTL'):Clone().
    clonepstl.Parent = game.Players.LocalPlayer.Backpack
end)

Hopefully this helps you and isn't too confusing. If there seems to be an error, feel free to tell me so I can fix it up if needed!

0
Thank you so much, it did help me a lot. Exteezy -3 — 5y
Ad

Answer this question