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

How do I get an item to "respawn" and still be able to trigger a ,Touched event?

Asked by 5 years ago

I am attempting to make an item (it is a union part that is stored in Server Storage by default) that gives the player more points when touched, then is destroyed and returns 5 seconds later. I am able to make the item give points when touched and get destroyed, but the problem is with the regenerating and being able to generate the .Touched event after the first time. My script currently spawns in the chest, and when touched gives my character more points, but does not respawn afterwards. I believe the issue is with the way I am identifying the object from the Server Storage or I am using the clone incorrectly. Any advice would be appreciated!

local trez50 = game.ServerStorage.TreasureChest50:Clone()
trez50.Parent = game.Workspace
trez50.Touched:Connect(function()
trez50:Destroy()
money.Value = money.Value + 50
wait(5)
trez50.Parent = game.Workspace
end)
0
Forgot to add, but I get this error, " The Parent property of TreasureChest50 is locked, current parent: NULL, new parent Workspace" after waiting the 5 seconds. I assume that only one clone is made and therefore when it is destroyed after being touched, the game is unable to access it. I now feel like I'm just having a big brainfart, but I would still love some help! toastedtoast224 26 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

There are a couple issues with your code, one issue with your script is you try to parent the object again after destroying it. The other is you're not verifying what's touching the object, meaning if anything touches it, the Touched Event will be executed. And finally, the way you do money doesn't seem to be based on every individual player, I won't correct in my code example, but it'd be best to access the money NumberValue of the player that touched the object, after we confirm if a player touched the object.

A better solution would be to create a function that clones the object, parents it, adds the Touched Event function (without the object being parented, after being destroyed), and calling the function whenever you want to spawn the object again.

Also, I recommend using workspace instead of game.Workspace, and using GetService when using services. Reason for workspace is due to conventions, while it isn't needed it's the usual way. And the reason to use GetService is due to services being able to be renamed.

local playerService = game:GetService("Players")
local serverStorage = game:GetService("ServerStorage")

local trez50 = serverStorage.TreasureChest50

function spawnTreasure()

    local clone = trez50:Clone()
    clone.Parent = workspace

    clone.Touched:Connect(function(hit)

        local parent = hit.Parent

        if (not parent:IsA("Model")) then return end --we don't want to check if the touched Part's parent is a Player, if the parent isn't a Model, since all Players' Characters are Models.

        local player = playerService:GetPlayerFromCharacter(parent) --check if the Model is a Character of a Player.

        if (player) then

            clone:Destroy()

            money.Value = money.Value + 50

            delay(5, function() spawnTreasure() end) --comment this out, if you don't want the coin to spawn after it's destroyed, and want to call it yourself sometime later.

        end

    end)

end

spawnTreasure() --comment this out if you want to call the function somewhere else.
Ad

Answer this question