Problem with Event

I have a local script that gives the player an amount of money every 10 seconds if the part is in workspace. The part is teleported to workspace when the button is pressed. However, it doesn’t print what it is supposed to, or do anything else.

if part.Parent == ws then
	print("poof")
	event:FireServer(plr)
end
event.OnServerEvent:Connect(function(plr)
	while true do
		sound:Play()
		print("yippee")
		plr.leaderstats.Money.Value += 2
		wait(10)
	end
end)
3 Likes

You can use a for loop that checks if the part.Parent == ws

1 Like

You can use a changed property to detect when the part.Parent is changed to ws

1 Like

What if i need it to be check infinitely?

1 Like

while task.wait() do

-- anything here repeats until you make it stop

end

1 Like

it still doesnt work
robloxapp-20231111-1755088.wmv (2.1 MB)

1 Like

the :FireServer event automatically puts the player as the first argument, you don’t need to specify it yourself

1 Like

Post your latest version of the script.

1 Like
event.OnServerEvent:Connect(function(plr)
	while task.wait() do
		sound:Play()
		print("yippee")
		plr.leaderstats.Money.Value += 2
		wait(10)
	end
end)
part.Parent.Changed:Connect(function()
	print("poof")
	event:FireServer()
end)
1 Like

Not sure how your doing things but these changes may help you:

LOCAL:

part.Changed:Connect(function(property)
	
	if property == "Parent" then
		print("poof")
		event:FireServer()
	end
	
end)

SERVER:

local reward = true

event.OnServerEvent:Connect(function(plr)
	
	while reward do
		reward = false
		
		-- check if part is in workspace
		
		if part.Parent == ws then
			sound:Play()
			print("yippee")
			plr.leaderstats.Money.Value += 2
			wait(10)
			reward = true
		else
			reward = false
			return
		end

	end
end)
1 Like

Still wouldn’t work, the dog just stands there and does nothing.

I made a quick file for you:

reward.rbxl (55.1 KB)

I hope you can turn it into what you need.

1 Like

Still wouldn’t work. I feel like the problem is the script that puts the part from repstorage to workspace

button.MouseButton1Click:Connect(function()
 	if player.leaderstats.Money.Value >= 5000 then 
		player.leaderstats.Money.Value -= 5000
		event:FireServer()
		task.wait(0.01)
		script:Destroy()
	elseif player.leaderstats.Money.Value <= 5000 then
		print("Unavailable Money")
		sound:Play()
	end
end)
local button = script.Parent
local event = button.DogEvent

event.OnServerEvent:Connect(function()
	local doge = game.ReplicatedStorage.Doge
	doge.Parent = game.Workspace
end)

What is not working?
What are you wanting to happen?

So, I want the part to give money to player, make a sound, and print something every 10 seconds but it wouldn’t do a single one.

Nope, this applies only to :FireClient(), not :FireServer(). You’ll have to specify the player.

Changing values on the client does not work, you’ll have to do this:

button.MouseButton1Click:Connect(function()
 	if player.leaderstats.Money.Value >= 5000 then 
		event:FireServer(player)
		script:Destroy()
	else
		print("Unavailable Money")
		sound:Play()
	end
end)
script.Parent.DogEvent.OnServerEvent:Connect(function(player)
    player.leaderstats.Money.Value -= 5000
	game.ReplicatedStorage.Doge:Clone().Parent = workspace
end)

I’m not sure what you mean by “does not work”, however, if you mean that the script is interfering with the script, then I don’t think so. I used the same script on a different item and it works perfectly.

Did you download the file I posted above?

You can see that it works and “how” it works.

Is the event in both scripts the same? You could be firing the wrong event.