Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
2

How do I Make my Coin Collect Script Only Make the Coin Dissapear on the LocalPlayer?

Asked by 3 months ago
Edited 3 months ago

whenever a player runs over a gem in my game, it disspears and they get a gem.

my question is, how do i make players run over my gem but the gem doesn't disspear to the whole server, it only dissapears to the client who collected the gem?

https://gyazo.com/75cbca62bd1ea9385199b409790bbc6f

my script:

function fade()
    repeat
        wait()
        script.Parent.Transparency = script.Parent.Transparency + 0.1
    until script.Parent.Transparency == 1
end


local db = true
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") ~= nil then
        if db == true then
            db = false
            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            player.leaderstats.Gems.Value = player.leaderstats.Gems.Value + 1
            script.Parent.Sound:Play()
            fade()
        end
    end
end)
0
Could you please put the script into a script block? superawesome113 110 — 3mo
0
ok i put the script into a script block 1swenzje 27 — 3mo
0
Destroy the part (gem) or set Transparency to 1 in the LocalScript,. You will need a RemoteEvent for that. Possible complication: You will probably have to ignore players on server, who have already collected that gem. sleazel 394 — 3mo

1 answer

Log in to vote
0
Answered by
sleazel 394 Moderation Voter
3 months ago

You need to change transparency in LocalScript, so it wont replicate to server and other clients... I also added a table, so each gem can be collected only once per player (I assume that was the intention?)

local ReplicatedStorage = game:GetService("ReplicatedStorage")

--following code (3 lines) needs to be run only once!
--if you intend to make multiple copies of this script (for other gems) 
--replace this code for each copy with
--local HideGemEvent = ReplicatedStorage:WaitForChild("HideGemEvent")
local HideGemEvent = Instance.new("RemoteEvent") --only once!!
HideGemEvent.Name = "HideGemEvent"  --only once!!
HideGemEvent.Parent = ReplicatedStorage  --only once!!


local playersWhoCollected = {}
local db = true
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") ~= nil then
        if db == true then
            db = false
            local player = game.Players:GetPlayerFromCharacter(hit.Parent)
            if playersWhoCollected[player] == nil then
                player.leaderstats.Gems.Value = player.leaderstats.Gems.Value + 1
                playersWhoCollected[player] = true
                HideGemEvent:FireClient(player,script.Parent)
                script.Parent.Sound:Play()
            end
            wait()
            db = true
        end
    end
end)

Add this LocalScript to StarterCharacterScripts (or where you normally keep local scripts, does not matter in this case)

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local HideGemEvent = ReplicatedStorage:WaitForChild("HideGemEvent")

local function OnHideGemEvent(gem)
    repeat
        wait()
        gem.Transparency = gem.Transparency + 0.1
    until gem.Transparency == 1
end
HideGemEvent.OnClientEvent:Connect(OnHideGemEvent)
0
omg thank you it works, ive been looking for a solution for like an hour 1swenzje 27 — 3mo
Ad

Answer this question