I’m making a coin script and need help making it local. Here is the thing though with my coins.
Whenever someone collects a coin, they cannot re-collect it forever. I’m also using DataStore 2. I have 2 scripts that control this, a local script in StarterPlayerScripts, and a script in ServerScriptService.
Here are the scripts:
ServerScriptService Script
local serverScriptService = game:GetService("ServerScriptService")
local dataStore2 = require(serverScriptService.DataStore2)
game.Players.PlayerAdded:Connect(function(player)
local CoinsGathered = dataStore2("CoinsGathered", player);
local coins = CoinsGathered:Get();
if(coins == nil) then
coins = {};
CoinsGathered:Set(coins);
end
game.ReplicatedStorage.RemoteEvents.coinManagerEvent:FireClient(player,coins);
end);
game.ReplicatedStorage.RemoteEvents.coinGatherEvent.OnServerEvent:Connect(function(player, coinID)
local coin;
local coins = game.Workspace.Coins:GetChildren();
for k,v in pairs(coins) do
if(v.CoinID.Value == coinID) then
coin = v;
break;
end
end
if(coin == nil) then
print("Not a valid coin!");
else
--[[print(player:DistanceFromCharacter(coin.Position));]]--
if(player:DistanceFromCharacter(coin.Position) < 35.0) then
local CoinsGathered = dataStore2("CoinsGathered", player)
local temp = CoinsGathered:Get();
table.insert(temp, coinID);
CoinsGathered:Set(temp);
local coinStore = dataStore2("Coins", player);
if(coinStore:Get() == nil) then
coinStore:Set(0);
end
local lobbyCoins = dataStore2("LobbyCoins", player)
lobbyCoins:Increment(1, player)
coinStore:Increment(coin.CoinValue.Value, player)
else
print("Player isn't near the coin!");
end
end
end)
Local Script in StarterPlayerScripts
local ReplicatedStorage = game:GetService("ReplicatedStorage");
local coinManagerEvent = ReplicatedStorage.RemoteEvents:WaitForChild("coinManagerEvent");
local coins = {};
-- This is triggered when the player loads into the game. This hides the coins already collected.
coinManagerEvent.OnClientEvent:Connect(function(coinList)
local allCoins = game.Workspace.Coins:GetChildren()
for k,v in pairs(coinList) do
for k2,v2 in pairs(allCoins) do
if(v == v2.CoinID.Value) then
v2:Destroy();
table.remove(allCoins, k2)
end
end
end
local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(
3, -- Time (25 is default)
Enum.EasingStyle.Linear, -- EasingStyle
Enum.EasingDirection.Out, -- EasingDirection
-1, -- RepeatCount (when less than zero the tween will loop indefinitely)
false, -- Reverses (tween will reverse once reaching it's goal)
0 -- DelayTime
)
local allCoins = game.Workspace.Coins:GetChildren()
for k,v in pairs(allCoins) do
local tween = TweenService:Create(v, tweenInfo, {CFrame = v.CFrame * CFrame.fromOrientation(0,0,math.rad(180))})
tween:Play()
local coinTouched = false
v.Touched:Connect(function(model)
local player = game.Players:GetPlayerFromCharacter(model.Parent)
if player then
if not coinTouched then
coinTouched = true
ReplicatedStorage.RemoteEvents.coinGatherEvent:FireServer(v.CoinID.Value)
v:Destroy();
table.remove(allCoins,k)
game:GetService("SoundService").SoundEffects.Coin:Play()
wait(1)
coinTouched = false;
end
end
end)
end
end)
I am guessing the problem is in the local script, but I don’t know what it is. There are no errors in the output, and it adds coins to both players if one of them collects it (not what I want). Any help will do.
Have a great day! ![]()