Help with Wins and crown adding

  1. I’m trying to make a script that will add a crown to a player if they have wins of 4

  2. It’s not adding the crown to the player

  3. *I have tried a lot of things

local Players = game:GetService("Players")

for _, player in pairs(Players:GetPlayers()) do
	local leaderstats = player:FindFirstChild("leaderstats")
		
	if leaderstats:FindFirstChild("Wins").Value == 4 then
		local humanoidRootPart = player.Character.HumanoidRootPart
				local crown = game.ServerStorage.Crown1:Clone()
		crown.Parent = game.Workspace
		
		if crown then
			local root = crown
			if root then
				local rotationSpeed = 1
				local distanceFromPlayer = 3
				local angle = 0

				game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
					if not crown or not root:IsDescendantOf(game) then
						return
					end

					angle = (angle + rotationSpeed * deltaTime) % (2 * math.pi)
					local newPosition = humanoidRootPart.Position + Vector3.new(math.cos(angle) * distanceFromPlayer, 0, math.sin(angle) * distanceFromPlayer)

					root:PivotTo(CFrame.new(newPosition) * CFrame.Angles(0, math.rad(angle * 360 / (2 * math.pi)), 0))
				end)
			else
				warn("Crown does not have a PrimaryPart or HumanoidRootPart.")
			end
		end
	end
end

If anyone knows the fix please let me know. Thanks

2 Likes

This for loop is only ran once when the script first executes.

Oh ok can you send me the fixed part of the script please?


local Players = game:GetService("Players")

local function checkWins(player,winNumber)
	--- the player had a change happen to the win value, let see what they can get now!
	if winNumber == 4 then
		local humanoidRootPart = player.Character.HumanoidRootPart
		local crown = game.ServerStorage.Crown1:Clone()
		crown.Parent = game.Workspace
		if crown then
			local root = crown
			if root then
				local rotationSpeed = 1
				local distanceFromPlayer = 3
				local angle = 0

				game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
					if not crown or not root:IsDescendantOf(game) then
						return
					end

					angle = (angle + rotationSpeed * deltaTime) % (2 * math.pi)
					local newPosition = humanoidRootPart.Position + Vector3.new(math.cos(angle) * distanceFromPlayer, 0, math.sin(angle) * distanceFromPlayer)

					root:PivotTo(CFrame.new(newPosition) * CFrame.Angles(0, math.rad(angle * 360 / (2 * math.pi)), 0))
				end)
			else
				warn("Crown does not have a PrimaryPart or HumanoidRootPart.")
			end
		end
	elseif winNumber == 10 then
		--- do some other cool things 
	end
end


local function setupPlayer(player)
	
	--- this will fire the checkWins function every time a player's win value changes.
	local leaderstats = player:FindFirstChild("leaderstats")
	local playerWins = leaderstats:FindFirstChild("Wins")
	playerWins.Changed:Connect(function()
		checkWins(player,playerWins.Value)
	end)
end

--- setup existing players
for _, player in pairs(Players:GetPlayers()) do
	setupPlayer(player)
end

--- setup new players when they join
Players.PlayerAdded:Connect(setupPlayer)

Try this

Oh wow it worked thank you so much

local serverStorage = game:GetService("ServerStorage")
local runService = game:GetService("RunService")
local crown = serverStorage.Crown1

local function checkWins(player, winNumber)
	if winNumber == 4 then
	    local character = player.Character
	    if not character then return end
	    local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
	    if not humanoidRootPart then return end
	    
		local crown2 = crown:Clone()
		crown2.Parent = workspace

		local angle = 0
		local connection
		connection = runService.Heartbeat:Connect(function(deltaTime)
		    if (not crown2.Parent) or (not player.Parent) then connection:Disconnect() end --disconnect connection
			angle = (angle + deltaTime) % (2 * math.pi)
			local newPosition = humanoidRootPart.Position + Vector3.new(math.cos(angle) * 3, 0, math.sin(angle) * 3)
			root:PivotTo(CFrame.new(newPosition) * CFrame.Angles(0, math.rad(angle * 360 / (2 * math.pi)), 0))
		end)
	elseif winNumber == 10 then
	    --other code
	end
end
for _, player in pairs(Players:GetPlayers()) do
	setupPlayer(player)
end

This part isn’t needed if the server script is executed when the server first starts.

OP should most likely make this a local script and not a server script as replicating PivotTo’s over the network isn’t very good . Also if you had a single wait for child in the script on the top it would break, I had to experience debugging a server bug because it was like a 50/50 chance it would catch the first player. Its good practice to add that part anyway. It runs once and doesn’t change anything.

qq do you know how to make it a GetOrderedDataStore not the wins value my data store is called “Wins” i want to give it to the player in first please and thanks.

replicating PivotTo’s over the network isn’t very good

I agree, they could set the network ownership of the ‘crown’ to the client too (so that it replicates to the other clients).

you had a single wait for child in the script on the top it would break

The server doesn’t need to wait for server-side instances to replicate.

Yo can you make it be get GetOrderedDataStore not values my data store is called “Wins” can you make it give the crown to the player in first please and thanks

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.