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

Collect-a-thon item collection problem: local changing or locking?

Asked by 5 years ago
Edited 5 years ago

So I have tried to work on the item that can be collected in a collect-a-thon however it's still not functioning the way I need it to,

the script is as follows:

local db = true
local itemGot = false

script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") ~= nil then -- check if a player has hit the object
        if db == true and itemGot == false then
            db = false
            script.Parent.Transparency = 0.125
            script.Parent.CanCollide = false
            script.Parent.Color = Color3.new(75,75,75)
            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            player.leaderstats2.Clapperboards.Value = player.leaderstats2.Clapperboards.Value + 1
            itemGot = true
        end
        wait(2)
        db = true
    end
end)

The idea is once player 1 (the first player to collect it) has got the item said player 1 can't get that item again, however any other players that are on the server who haven't collected this item can still get it.

Note: I have added another variable to try and see if it would work but the script stops any other players collecting once player 1 collects it.

0
So to make it clear what I want the script to do. Let's say you have a server of 8 players and 1 player collects the item. I still want the other 7 players to be able to collect the item but not the 1 player that collected it. Lucarios3 4 — 5y
0
If you mean for the item to disappear (or transparent to .125), be non-collectible, and color change ONLY for the person who touches it. I would suggest Remote Events GoodCallMrOlsen 70 — 5y
0
Please give us more context, are there going to be more than 1 of this object? Does it need to be saved or just stay that way until the player joins again? DinozCreates 1070 — 5y
0
ok Dino, so the idea of the item is like the stars in Mario 64 (I haven't got a save feature yet and the +1 on the leader stats is to check that it works) Lucarios3 4 — 5y

1 answer

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

What you're trying to do

The idea is once player 1 (the first player to collect it) has got the item said player 1 can't get that item again, however any other players that are on the server who haven't collected this item can still get it.

(psss, hey there, if this answer helped you should accept)

Problem with that

itemGot is only false once. The moment a player touches the item, itemGot is set to true and never set back to false.

Solution

We can use a dictionary for this case to store the players who have collected the item. And we can check if the player touching the item is already in the playersWhoCollected dictionary (or whatever you decide to name the variable).

Example

local players = game:GetService("Players")

local canTouch = true
local playersWhoCollected = {}

local item = script.Parent

local function itemTouched(hit)
    if canTouch then
        canTouch = false

        local humanoid = hit.Parent:FindFIrstChild("Humanoid")

        if humanoid then
            local character = humanoid.Parent
            local player = players:GetPlayerFromCharacter(character)

            if player then
                if not playersWhoCollected[player] then
                    --// update player leader stats
                    playersWhoCollected[player] = true
                end
            end
        end

        wait(2)
        canTouch = true
    end
end

item.Touched:Connect(itemTouched)

Now for making the item disappear only for the person who touched we would want to use remote events. GoodCallMrOlson has pointed this out in the comments.

If you mean for the item to disappear (or transparent to .125), be non-collectible, and color change ONLY for the person who touches it. I would suggest Remote Events

Pseudo code you could use: makeItemInvisible:FireClient(client, item). Have a remote for making an item invisible and fire the client and have them make it invisible on their client.

Example client code

local makeItemInvisibleEvent = ... --// reference to remote event

local function makeItemInvisible(item)
    item.Transparency = .125
    item.CanCollide = false
    item.Color = Color3.new(75, 75, 75) --// notice I used `Color3.fromRGB` and not `Color3.new` as you did since `Color3.new` accepts ranges from `[0, 1]` while `Color3.fromRGB` accepts ranges from `[0, 255]`
end

makeItemInvisibleEvent.OnClientEvent:Connect(makeItemInvisible)
Ad

Answer this question