I have a system where when an enemy dies, it drops some coins. I then want to make a system where the coins attract to the player when near, but for some reason the part’s position cannot be changed.
I figure this is due to something in the function that spawns them so here’s that:
function CurrencyClient.SpawnCoinsOnFloor(enemy, expvalue, coinAmount, coinValue)
if enemy == nil then return end
if coinAmount <= 0 then return end
local coinsToSpawn = tonumber(coinAmount)
for coinsSpawned = coinsToSpawn, 0, -1 do
if coinsToSpawn <= 0 then continue end
coinsToSpawn -= 1
local coinToSpawn = coin:Clone()
coinToSpawn.PrimaryPart.Orientation = Vector3.new(0, 0, 0)
coinToSpawn.Parent = game.Workspace.Clutter.Coins
coinToSpawn:SetAttribute("Value", coinValue)
local enemyPos = enemy.PrimaryPart.Position
coinToSpawn.PrimaryPart.Position = enemyPos
local coinPos = coinToSpawn.PrimaryPart.Position
local spreadAmount = 3
local spawnCFrames = {...} -- Long table I cut out to save space
local rand = math.random(1, #spawnCFrames)
local randoms1 = math.random(-6, 6)
local randoms3 = math.random(-6, 6)
local randomCFrame: CFrame = spawnCFrames[rand]
local randomX = randomCFrame.X + randoms1
local randomZ = randomCFrame.Z + randoms3
local TargetPosition = Vector3.new(randomX, randomCFrame.Y, randomZ)
local StartPosition = enemyPos
local Tween: Tween = CurrencyClient.CreateTween(coinToSpawn, TargetPosition)
Tween:Play()
local StartTime = tick()
RunServiceEvent = runService.RenderStepped:Connect(function()
local Time = (tick() - StartTime) / tweenInfo.Time
if coinToSpawn.PrimaryPart == nil then return end
if Time > 1 then
coinToSpawn.PrimaryPart.Position = TargetPosition
return
end
coinToSpawn.PrimaryPart.Position = CurrencyClient.CurvePath(StartPosition, TargetPosition, Height)(Time)
end)
task.spawn(function()
Tween.Completed:Wait()
task.wait()
coins = game.Workspace.Clutter.Coins:GetChildren()
RunServiceEvent = nil
end)
end
end
I can’t update the coin’s position via script either (I’m only using local scripts/modules for this as I only want coins to be client-side)
Edit: heres a file with a test place where I recreated the problem as simply as possible, press the text button and then use the move tool to try and move the part that spawns and you cannot: testplace.rbxl (73.6 KB)
Alright, I think I have one theory. You have the runservice.renderstepped, which isn’t ending. The issue here is that it’ll loop forever and setting same position over and over again, so you can’t move it.
Solution: When tweening is completed and position is “stable”, disconnect runservice event and issue should be solved.
Is what it looks like now, still same outcome as before. Changing it to disconnect in the testplace also doesn’t fix it so I don’t know if that’s the issue
Well, I got something working. Apparently you can’t move part created by modulescript for some obscure reason. You can try detecting when part is added in Cluster.Coins folder and move the part something like 0.001 studs, which sets the “owner” to localscript. Therefor, you can now move the part using move tools in studio.
Yanky fix, but that’s what i got working
Strange, In my main project I try to move it in the same module script after the tween, so can you not move the part within the same module script it was created by?
Theres something to do with how modulescript sets networkowner of the part, I think.
You can try making seperate server script, which will receive remoteEvent and sets the networkownership to player. I’m not sure if this will work, but easiest way to fix this issue is to use the yanky answer I gave before.
If you don’t mind, here’s my version of the testPlace now where the bottom button moves a coin part, from within the same module script, after you’ve spawned some in: testplace2.rbxl (73.9 KB)
This would probably need a whole other post but in my main project, I have a function that is connected when the player gets near to a coin, then it plays a tween and is meant to move that coin into the player then dissapear, but it doesn’t move the coin even though the tween gets played, so I don’t know why now in the test place I can move a part within the same script but I can’t in my main project.
This is getting quite complicated… I guess there might be other people who can help with this, and I would in this case, create new post, as you said above.
Other post I made got deleted so here’s the solution:
In the RunServiceEvent just remove the if Time > 1 then check and everything in it, then the code works just the same and the part is able to be moved afterwards.
For some reason, setting the RunServiceEvent to nil or disconnecting it did not stop it from constantly setting the parts position in that if check