Coin not working

Hey! For my game I have made a coin system. The coins spawning process across the map is fine. But it is having some issues with rotating and detecting if a player hits it.

Video: https://gyazo.com/00f2572b620c1154651b1c323ea4d9cc

Here is the script:

local TweenService = game:GetService("TweenService")
local Coin = script.Parent
local PIP = {}
local fg = require(game.ServerScriptService.FastGet)
local alrC = false

function RotateCoin()
	local tweeninfo = TweenInfo.new(0.5,Enum.EasingStyle.Linear,Enum.EasingDirection.InOut)
	local tweenA = TweenService:Create(Coin,tweeninfo,{Orientation=Vector3.new(180,0,0)})
	local tweenB = TweenService:Create(Coin,tweeninfo,{Orientation=Vector3.new(360,0,0)})
	
	tweenA:Play()
	wait(0.5)
	tweenB:Play()
end

function RTP()
	for i, v in pairs(PIP) do
		if v.Parent:FindFirstChildWhichIsA("Humanoid") then
			alrC = true
			local ID = game.Players:GetNameFromUserIdAsync(v.Parent.Name)
			local CoinV = fg(ID,"Coin") -- Gets the coin IntValue in PlayerData fp;der
			CoinV.Value = CoinV.Value + 1 -- adds a coin to the playerData
		end
	end
end


while true do
	task.wait(1)
	RotateCoin()
	PIP = workspace:GetPartsInPart(Coin)
	if PIP then
		local suc, err = pcall(RTP)
		if suc and alrC then Coin:Destroy() end
	end
end
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")

local Coin = workspace.Coin 

local r = math.rad 
function RotateCoin(coin)
	coroutine.wrap(function()
		while task.wait() do 
			coin.CFrame *= CFrame.Angles(r(0), r(1), r(0))
		end
	end)()
end

--call once for each coin
--also rotate the coin on the client, if you want it to be smooth
RotateCoin(Coin)

--use events for collisions(assuming the player character isn't anchored)
Coin.Touched:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if not player then return end 
	local char = hit.Parent 
	print(char.Name.." collided with coin!")
end)

1 Like

I modified the code a bit to make it compatable with my current system in place

local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")

local Coin = script.Parent

local r = math.rad 
function RotateCoin(coin)
	coroutine.wrap(function()
		while task.wait() do 
			coin.CFrame *= CFrame.Angles(r(0), r(1), r(0))
		end
	end)()
end

--call once for each coin
RotateCoin(Coin)

--use events for collisions(assuming the player character aren't anchored)
Coin.Touched:Connect(function(hit)
	local player = Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		local char = hit.Parent 
		local coinV = require(game.ServerScriptService.FastGet)(player.UserId,"Coin")
		coinV.Value += 1
		Coin:Destroy()
	end
end)

Also to avoid creating multiple threads for rotating each coin you can also do:

coroutine.wrap(function()
	while task.wait() do 
		for _, coin in pairs(coins) do 
			coin.CFrame *= CFrame.Angles(r(0), r(1), r(0))
		end 
	end
end)()

where coins is an array containing all the coins.